Search code examples
phparraysmultidimensional-arrayphp-5.3

Get the max array from an array multidimensional based on value of column


I have this array call $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
    )

)

Now I want to get the array who have the max number of column totalGames, in this case I want to get the array with the key 682. I tried like this $aMaxGame= max($aSumGame['totalGames']); but not work. Can you help me please ?


Solution

  • You can use uasort along with current function like as

    uasort($arr,function($a,$b){
        return $b['totalGames'] - $a['totalGames'];
    });
    
    print_r(current($arr));
    

    You can simply use usort like as

    usort($arr,function($a,$b){
        return $b['totalGames'] - $a['totalGames'];
    });
    
    print_r($arr[0]);
    

    Demo