Find the Maximum Value in a Multidimensional Array : Multidimensional Array « Data Structure « PHP





Find the Maximum Value in a Multidimensional Array

 
<?php
function recursive_array_max($a) {
    foreach ($a as $value) {
        if (is_array($value)) {
            $value = recursive_array_max($value);
        }
        if (!(isset($max))) {
            $max = $value;
        } else {
            $max = $value > $max ? $value : $max;
        }
    }
    return $max;
}
$dimensional = array(
    7,
    array(3, 5),
    array(5, 4, 7, array(3, 4, 6), 6),
    14,
    2,
    array(5, 4, 3)
    );

$max = recursive_array_max($dimensional);

echo "<p>The maximum value was: {$max}</p>";
?>
  
  










Related examples in the same category

1.Reference element in multidimensional Arrays
2.Defining a Multidimensional Array
3.Looping Through a Multidimensional Array
4.Displaying a Multidimensional Array using For each
5.Accessing multidimensional array elements
6.Creating Multidimensional Arrays
7.Creating a Multidimensional Array
8.Sorting Multidimensional Arrays
9.Multidimensional array element value interpolation
10.Displaying Multidimensional Arrays
11.Displaying a multidimensional array
12.Generating fixed-width field data records without
13.Manipulating multidimensional arrays