How can the code be refactored to efficiently determine the minimum and maximum values of a multidimensional array?

To efficiently determine the minimum and maximum values of a multidimensional array in PHP, we can iterate through the array using nested loops and keep track of the minimum and maximum values found. By initializing the min and max variables with the first element of the array, we can compare each element with the current min and max values and update them accordingly.

function findMinMax($arr) {
    $min = $arr[0][0];
    $max = $arr[0][0];
    
    foreach ($arr as $subArr) {
        foreach ($subArr as $val) {
            if ($val < $min) {
                $min = $val;
            }
            if ($val > $max) {
                $max = $val;
            }
        }
    }
    
    return ['min' => $min, 'max' => $max];
}

// Example usage
$multiArray = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

$result = findMinMax($multiArray);
echo "Minimum value: " . $result['min'] . "\n";
echo "Maximum value: " . $result['max'] . "\n";