0

It is the result of fetch array how can i find min & max from this array.

Array ( [0] => 0 [pr] => 0 ) 

Array ( [0] => 200 [pr] => 200 ) 

Array ( [0] => 250.1 [pr] => 250.1 ) 

Array ( [0] => 500 [pr] => 500 ) 

Array ( [0] => 800 [pr] => 800 )

Array ( [0] => 5656 [pr] => 5656 ) 

Array ( [0] => 34343 [pr] => 34343 )

Array ( [0] => 565656 [pr] => 565656 )
jeni
  • 442
  • 1
  • 4
  • 12
  • 1
    Use "fetch column" instead of "fetch array", and then it's trivial to do it with the `min` and `max` functions. It doesn't make sense to flatten the array, that's the equivalent of ordering cold food and heating it up afterwards. You should order it hot in the first place. – Jon Jul 26 '11 at 12:16

4 Answers4

1
$max = max(array_map('current', $array));
$min = min(array_map('current', $array));
deceze
  • 510,633
  • 85
  • 743
  • 889
0

It looks like maybe you have an array of arrays, each inner array has two keys with the same value, and you want the min and max of the whole set of values. If this is the case, you might want to flatten the array, so you get a single array with just the values, and then use the PHP min and max functions. They can take arrays as parameters.

The flatten function (copied from the link I gave):

function flatten(array $array) {
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
    return $return;
}

How you'd use it:

// assuming $the_array is your array of arrays:

$flat_array = flatten($the_array);
$min = min($flat_array);
$max = max($flat_array);
Community
  • 1
  • 1
Philip
  • 694
  • 5
  • 14
0

It is not entirely clear how the lines are connected, but I assume we're talking a multidimensional array here. In that case it would be easy to iterate:

$max = null;
foreach ($outer as $inner) {
    $max = max($max, $inner[0]); // Compare with previous max
}

// $max is now 565656
jensgram
  • 31,109
  • 6
  • 81
  • 98
0
 $min = $max = 0;
 foreach ($arrays as $arr)
 {
      if($arr[0] < $min) { $min = $arr[0]; }
      if($arr[0] > $max) { $max = $arr[0]; }
 }

 var_dump($min);
 var_dump($max);