Php 多维数组的排序与过滤
我需要用这个多维数组捕捉每个国家的老年人Php 多维数组的排序与过滤,php,Php,我需要用这个多维数组捕捉每个国家的老年人 $arrayPersonas = array( array('id'=> "0AB239", 'country' => 1, 'firstname' => "Ernest" , 'surname' => "Austin", 'age' => 30), array('id'=> "12A179", 'country' =&g
$arrayPersonas = array(
array('id'=> "0AB239", 'country' => 1, 'firstname' => "Ernest" , 'surname' => "Austin", 'age' => 30),
array('id'=> "12A179", 'country' => 0, 'firstname' => "Frances", 'surname' => "Poole", 'age' => 23),
array('id'=> "1A90B9", 'country' => 1, 'firstname' => "Jacob", 'surname' => "Matthews", 'age' => 30),
array('id'=> "227FF9", 'country' => 2, 'firstname' => "Mina", 'surname' => "Day", 'age' => 30),
array('id'=> "2A6F39", 'country' => 3, 'firstname' => "Earl", 'surname' => "Mills", 'age' => 20),
array('id'=> "325E79", 'country' => 4, 'firstname' => "Dennis", 'surname' => "Ray", 'age' => 33),
array('id'=> "32E478", 'country' => 4, 'firstname' => "Alex", 'surname' => "Fery", 'age' => 33),
array('id'=> "3A4DB9", 'country' => 5, 'firstname' => "Rhoda", 'surname' => "Conner", 'age' => 35));
我还必须显示该数组的所有内容,因为我必须显示ID、Country、Firstname等
我试着按国家分类,然后选择年龄最大的人,但失败了
function filtrar_por($data){
array_filter($data, function( $var ){
for($i=0; $i <= 5; $i++){
switch ($i) {
case 0:
$country0 = $var['country'] == $i;
$country0 = ordenar_por($country0);
echo $country0;
break;
case 1:
$country1 = $var['country'] == $i;
break;
case 2:
$country2 = $var['country'] == $i;
break;
case 3:
$country3 = $var['country'] == $i;
break;
case 4:
$country4 = $var['country'] == $i;
break;
case 5:
$country5 = $var['country'] == $i;
break;
}
}
});
}
function ordenar_por($data){
foreach ($data as $clave => $fila) {
$orden1[$clave] = $fila['age'];
}
array_multisort($orden1, SORT_DESC, $data);
return $data;
}
函数过滤器(数据){
数组过滤器($data,function($var){
对于($i=0;$i$fila){
$orden1[$clave]=$fila['age'];
}
数组_multisort($orden1,SORT_DESC,$data);
返回$data;
}
我尝试使用此命令查看是否有效,但没有任何效果。使用usort,如下所示:
<?php
$arrayPersonas = array(
array('id'=> "0AB239", 'country' => 1, 'firstname' => "Ernest" , 'surname' => "Austin", 'age' => 30),
array('id'=> "12A179", 'country' => 0, 'firstname' => "Frances", 'surname' => "Poole", 'age' => 23),
array('id'=> "1A90B9", 'country' => 1, 'firstname' => "Jacob", 'surname' => "Matthews", 'age' => 30),
array('id'=> "227FF9", 'country' => 2, 'firstname' => "Mina", 'surname' => "Day", 'age' => 30),
array('id'=> "2A6F39", 'country' => 3, 'firstname' => "Earl", 'surname' => "Mills", 'age' => 40),
array('id'=> "325E79", 'country' => 4, 'firstname' => "Dennis", 'surname' => "Ray", 'age' => 33),
array('id'=> "32E478", 'country' => 4, 'firstname' => "Alex", 'surname' => "Fery", 'age' => 50),
array('id'=> "3A4DB9", 'country' => 5, 'firstname' => "Rhoda", 'surname' => "Conner", 'age' => 35));
$final_data = array();
function sortByOrder($a, $b) {
return $a['age'] + $b['age'];
}
foreach( $arrayPersonas as $person){
if(!empty($final_data[$person['country']])){
foreach( $final_data[$person['country']] as $key => $value ){
if( $final_data[$person['country']][$key]['age'] < $person['age'] ){
unset($final_data[$person['country']][$key]);
$final_data[$person['country']][] = $person;
}else if( $final_data[$person['country']][$key]['age'] == $person['age'] ){
$final_data[$person['country']][] = $person;
}
}
}else{
$final_data[$person['country']][] = $person;
usort($final_data[$person['country']], 'sortByOrder');
}
}
print_r($final_data);
我有另一种方法来找到你想要的结果。它可能比Burhan anwser效率稍低,但我认为它更具可读性
<?php
$persons = array(
array('id'=> "0AB239", 'country' => 1, 'firstname' => "Ernest" , 'surname' => "Austin", 'age' => 30),
array('id'=> "12A179", 'country' => 0, 'firstname' => "Frances", 'surname' => "Poole", 'age' => 23),
array('id'=> "1A90B9", 'country' => 1, 'firstname' => "Jacob", 'surname' => "Matthews", 'age' => 30),
array('id'=> "227FF9", 'country' => 2, 'firstname' => "Mina", 'surname' => "Day", 'age' => 30),
array('id'=> "2A6F39", 'country' => 3, 'firstname' => "Earl", 'surname' => "Mills", 'age' => 20),
array('id'=> "325E79", 'country' => 4, 'firstname' => "Dennis", 'surname' => "Ray", 'age' => 33),
array('id'=> "32E478", 'country' => 4, 'firstname' => "Alex", 'surname' => "Fery", 'age' => 50),
array('id'=> "3A4DB9", 'country' => 5, 'firstname' => "Rhoda", 'surname' => "Conner", 'age' => 35)
);
// Get the different countries you can have
$differentCountries = array_unique(array_column($persons, "country"));
$result = [];
// Loop over the different contries
foreach ($differentCountries as $countryId) {
// Get the all values for this specific country
$valuesForThisCountry = array_filter($persons, function ($person) use ($countryId) {
return $person["country"] == $countryId;
});
// Get an associative array with "id" as key and "age" as value
$formattedArray = array_column($valuesForThisCountry, "age", "id");
// Get the older ones
$olders = array_keys($formattedArray, max($formattedArray));
// Get the initial values of persons you found
$personsById = array_filter($persons, function ($person) use ($olders) {
// Check if the "id" is in the $olders
return in_array($person["id"], $olders);
});
// Finally, put the result in the $result array
$result = array_merge($result, $personsById);
}
print_r($result);
你能告诉我们你尝试了什么吗?你需要的结果到底是什么?你只需要知道谁的年龄大?或者把数组从大到小排序?函数ordenar_por($data){foreach($clave=>$fila){$orden1[$clave]=$fila['age'];}array\u multisort($orden1,sort\u DESC,$data);返回$data;}@Burnakashour我只需要向老年人展示所有信息和订单ID@CornelRaiu在主要帖子上编辑谢谢,但我需要显示每个帖子中的每个年长者country@Zano你说的我只需要按ID显示年长者的所有信息和订单
对不起,我弄错了。在主要帖子上,我解释的第一行。@Zano更新请注意看!例如,国家1有两个人是同一年的人,你怎么能同时显示这两个人?但这并不能回答他提出的全部问题,他希望完整的数组被索引为国家数组,每个国家都是一个老年人数组,如果同一个国家有相同的年龄,那么你应该将他们添加到数组中。我更新了代码片段,我认为这一次是正确的;)