根据列的值从多维数组中获取最大数组
Get the max array from an array multidimensional based on value of column
我有这个数组调用 $aSumGame
:
Array
(
[682] => Array
(
[nature] => 682
[totalGames] => 3
[category] => super
)
[707] => Array
(
[nature] => 707
[totalGames] => 2
[category] => event
)
[728] => Array
(
[nature] => 728
[totalGames] => 2
[category] => event
)
)
现在我想获取具有最大列数 totalGames
的数组,在本例中我想获取具有键 682
的数组。我试过这样 $aMaxGame= max($aSumGame['totalGames']);
但没有用。你能帮帮我吗?
您可以将 uasort
与 current
函数一起使用,例如
uasort($arr,function($a,$b){
return $b['totalGames'] - $a['totalGames'];
});
print_r(current($arr));
您可以像
一样简单地使用 usort
usort($arr,function($a,$b){
return $b['totalGames'] - $a['totalGames'];
});
print_r($arr[0]);
检查一下,还没有 运行 但应该没问题:
$array = array('682'=>array('nature'=>1,'totalGames'=>3), '707'=>array('nature'=>1,'totalGames'=>2));
$tempArray = array();
foreach($array as $id=>$array2) {
$tempArray[$array2['totalGames']][] = $id;// store by the number of games, ex : array('2'=>array(707, 728), '3'=>array(682)
}
$maxKey = max(array_keys($tempArray)); //get the max, ex: 3
var_dump($tempArray[$maxKey]); //all id`s with max, ex: array(682)
list($oneResult) = $tempArray[$maxKey]; //get 682
var_dump($array[$oneResult]); //element from the initial array from key
试试这个
usort($array, function($a, $b) {
return strnatcasecmp($b['totalGames'], $a['totalGames']);
});
print_r(current($array));
我有这个数组调用 $aSumGame
:
Array
(
[682] => Array
(
[nature] => 682
[totalGames] => 3
[category] => super
)
[707] => Array
(
[nature] => 707
[totalGames] => 2
[category] => event
)
[728] => Array
(
[nature] => 728
[totalGames] => 2
[category] => event
)
)
现在我想获取具有最大列数 totalGames
的数组,在本例中我想获取具有键 682
的数组。我试过这样 $aMaxGame= max($aSumGame['totalGames']);
但没有用。你能帮帮我吗?
您可以将 uasort
与 current
函数一起使用,例如
uasort($arr,function($a,$b){
return $b['totalGames'] - $a['totalGames'];
});
print_r(current($arr));
您可以像
一样简单地使用usort
usort($arr,function($a,$b){
return $b['totalGames'] - $a['totalGames'];
});
print_r($arr[0]);
检查一下,还没有 运行 但应该没问题:
$array = array('682'=>array('nature'=>1,'totalGames'=>3), '707'=>array('nature'=>1,'totalGames'=>2));
$tempArray = array();
foreach($array as $id=>$array2) {
$tempArray[$array2['totalGames']][] = $id;// store by the number of games, ex : array('2'=>array(707, 728), '3'=>array(682)
}
$maxKey = max(array_keys($tempArray)); //get the max, ex: 3
var_dump($tempArray[$maxKey]); //all id`s with max, ex: array(682)
list($oneResult) = $tempArray[$maxKey]; //get 682
var_dump($array[$oneResult]); //element from the initial array from key
试试这个
usort($array, function($a, $b) {
return strnatcasecmp($b['totalGames'], $a['totalGames']);
});
print_r(current($array));