How to get maximum value in inner array elements in php -
this might easy not me
i have var_dump($array)
this.
array (size=12) 0 => int 1 1 => int 7 2 => int 8 3 => int 9 4 => int 3 5 => int 8 6 => int 3 7 => int 6 8 => int 5 9 => int 3 10 => int 4 11 => int 5
i need max value index 2 5 , index 4 8 , 8 11
how can acheive this. tried max()
function getting errors.
you can iterate through array searching max value. first set var $max
negative value , inside loop if value in index $i
greater $max
, change value.
<?php $array = array( 0 => 1, 1 => 7, 2 => 8, 3 => 9, 4 => 3, 5 => 8, 6 => 3, 7 => 6, 8 => 5, 9 => 3, 10 => 4, 11 => 5 ); function maxarray($from, $to, $array) { $max = -1; for($i = $from; $i <= $to; $i++) { if($array[$i] > $max) { $max = $array[$i]; } } return $max; } echo maxarray(2, 5, $array) . "<br>"; echo maxarray(4, 8, $array) . "<br>"; echo maxarray(8, 11, $array) . "<br>"; ?>
output:
9 8 5