[英]Order and filter multidimensional array
我需要用這個多維數組抓住每個國家的老年人
$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、國家/地區、名字等。
我試圖按國家排序,然后選擇最年長的人,但我失敗了
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;
}
我試着用這個看看是否有效,但沒有。
像下面這樣使用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);
代碼片段沙箱: http : //sandbox.onlinephpfunctions.com/code/8ffed1ebb2940fc71cd35cb8bfddae2e8ca3077c
我有另一種方法來找到你想要的結果。 它可能比 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);
http://sandbox.onlinephpfunctions.com/code/464bf5fa58f75bdecbf85f49edfecefd3df90673
編輯:作為 anwsered Burhan Kashour,我編輯了代碼(只是編輯了最后一個合並行)
這是新版本http://sandbox.onlinephpfunctions.com/code/cb757dc7ae4862f6ac671f0cb36e057f18a8ced6
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.