Find the highest value within array

So, I have the following array:


$thumbs = array (
    1 => array (
            1 => array (
                    'src' => 'img/ss1.gif',
            	    'width' => '440',
            	    'height' => '307',
            	    'alt' => ''
                ),
            2 => array (
                    'src' => 'img/ss2.gif',
                    'width' => '440',
                    'height' => '312',
            	    'alt' => ''
                ),
            3 => array (
                    'src' => 'img/ss3.gif',
            	    'width' => '440',
            	    'height' => '318',
            	    'alt' => ''
                ),
            4 => array (
                    'src' => 'img/ss4.gif',
            	    'width' => '440',
            	    'height' => '318',
            	    'alt' => ''
                )
        ),
    2 => array (
             1 => array (
                    'src' => 'img/ap-ss1.gif',
             	    'width' => '440',
            	    'height' => '295',
            	    'alt' => ''
                ),
             2 => array (
                    'src' => 'img/ap-ss2.gif',
            	    'width' => '440',
            	    'height' => '317',
            	    'alt' => ''
                ),
             3 => array (
                    'src' => 'img/ap-ss3.gif',
            	    'width' => '440',
            	    'height' => '296',
            	    'alt' => ''
                )
        ),
    3 => array (
             1 => array (
                    'src' => 'img/s-ss1.jpg',
             	    'width' => '440',
            	    'height' => '258',
            	    'alt' => ''
                ),
             2 => array (
                    'src' => 'img/s-ss2.jpg',
             	    'width' => '440',
            	    'height' => '252',
            	    'alt' => ''
                ),
            3 => array (
                    'src' => 'img/s-ss3.jpg',
             	    'width' => '440',
            	    'height' => '293',
            	    'alt' => ''
                )
        )
);

I’m trying to implement a function to return the highest ‘height’ within the contents of a given index. For instance, if id equals to 1, the return value is 318.

I’ve come up with this:


 function max_height($id, $array) {
          $val_aux = 0;
          foreach($array[$id] as $key => $value) {
              $val = $value['height'];
              if ($val > $val_aux) {
                  $val_aux = $val;
              }
          }
          return $val_aux;
      }

It works, but I was wondering if anyone has a better/more elegant way to do this.

http://php.net/manual/en/function.max.php

Be VERY careful there, Legend. the max function in this case will only work if all the widths are equal - otherwise, it will return the array element with the greatest width, not height.

Incidentally, if you have the ability to restructure the array, putting height ‘first’ in the array will let max() work as you want it to, Sam.

Thanks Sgt and SL.


<?php
function get_max($index, $array){
    $array = $array[$index];
    usort($array, function($a, $b){
        return $a['height'] < $b['height'];
    });
    return current($array);
}


var_dump(
    get_max(1, $thumbs)
);


/*
    array(4) {
        ["src"]=>
        string(11) "img/ss4.gif"
        ["width"]=>
        string(3) "440"
        ["height"]=>
        string(3) "318"
        ["alt"]=>
        string(0) ""
    }
*/
?>

Anthony, that doesn’t do what Sam32 was looking to do. See his own example which he just wants to do more “elegantly”. Of course, having the whole array with the highest height makes it easy to get that height. (:

I made a slight change to return the max height directly.


function get_max($index, $array){
    $array = $array[$index];
    usort($array, function($a, $b){
        return $a['height'] < $b['height'];
    });
    $array = current($array);
    return $array['height'];
}

Thanks. :slight_smile:

Instead of


if ($val > $val_aux) {
   $val_aux = $val;
}

you can use


$val_aux = max($value, $val_aux);

Which is a little shorter and a bit more readable (and I’m pretty sure does exactly the same thing “under the hood”, but okay :))

As for your custom comparison function, you should return $a['height'] - $b['height'], not $a['height'] < $b['height'], since a custom sorter should return a negative number if the first argument is smaller than the second argument, a positive integer if the first argument is bigger than the second argument and zero if they are equal. Your function will only return true or false, which can be juggled to 0 and 1 so it might sort of work, but you’re missing the case where the first argument is smaller than the second (in which case your function will incorrectly report that both arguments are equal).