Get the maximum value from an element in a multidimensional array?

前端 未结 5 1334
轻奢々
轻奢々 2020-11-28 14:17

I\'m trying to select the maximum value for a particular key in a multidimensional array. I\'m having trouble \"getting to\" the key in question...

So, the array (

相关标签:
5条回答
  • 2020-11-28 15:03
    $max = 0;
    foreach($array as $obj)
    {
        if($obj->dnum > $max)
        {
            $max = $obj->dnum;
        }
    }
    

    That function would work correctly if your highest number is not negative (negatives, empty arrays, and 0s will return the max as 0).

    Because you are using an object, which can have custom properties/structures, I don't believe there are really any 'predefined' functions you can use to get it. Might as well just use a foreach loop.

    You really can't get away from a foreach loop, as even internal functions use a foreach loop, it is just behind the scenes.

    Another solution is

    $numbers = array();
    foreach($array as $obj)
    {
        $numbers[] = $obj->dnum;
    }
    $max = max($numbers);
    
    0 讨论(0)
  • 2020-11-28 15:06

    In PHP 5.2 the only way to do this is to loop through the array.

    $max = null;
    foreach ($arr as $item) {
      $max = $max === null ? $item->dnum : max($max, $item->dnum);
    }
    

    Note: I've seeded the result with 0 because if all the dnum values are negative then the now accepted solution will produce an incorrect result. You need to seed the max with something sensible.

    Alternatively you could use array_reduce():

    $max = array_reduce($arr, 'max_dnum', -9999999);
    
    function max_denum($v, $w) {
      return max($v, $w->dnum);
    }
    

    In PHP 5.3+ you can use an anonymous function:

    $max = array_reduce($arr, function($v, $w) {
      return max($v, $w->dnum);
    }, -9999999);
    

    You can use array_map() too:

    function get_dnum($a) {
      return $a->dnum;
    }
    
    $max = max(array_map('get_dnum', $arr));
    
    0 讨论(0)
  • 2020-11-28 15:09

    If you like oneliners

    $max = max( array_map(function( $row ){ return $row->dnum; }, $myarray) );
    
    0 讨论(0)
  • 2020-11-28 15:14

    For anyone still interested, try this...

     $min = min(array_column($myarray, 'dnum'));
     $max = max(array_column($myarray, 'dnum'));
    
    0 讨论(0)
  • 2020-11-28 15:15

    The simplest way is probably your initial thought, which is to loop your array once, and pull out all the dnum keys into a separate array, then call max() on that:

    $out = array();
    foreach($myarray as $obj) {
        $out[] = $obj->dnum;
    }
    echo max($out);
    

    You could do it without creating a separate array, but you'll end up calling max() a lot more often. Performance/memory usage will be different between the two, you could always benchmark it:

    $first = $myarray[0];  //assume zero start index
    $max = $first->dnum;
    foreach($myarray as $obj) {
        $max = max($max,$obj->dnum);
    }
    echo $max;
    

    The only other way you could go about it would be to sort your array using usort() and a custom sorting function based on the object dnum properties. This is probably going to be much slower than just looping your array however, so I don't think I'd recommend it unless you needed the array sorted as a side effect.

    0 讨论(0)
提交回复
热议问题