Home > Backend Development > PHP Problem > Find the maximum value of a two-dimensional array in php

Find the maximum value of a two-dimensional array in php

王林
Release: 2023-05-05 21:16:06
Original
1867 people have browsed it

In PHP's two-dimensional array, we can use a variety of methods to find the maximum value of the two-dimensional array. In this article, three methods will be introduced to achieve this requirement: method one uses loop iteration, method two uses recursion, and method three uses PHP built-in functions.

Method 1: Using loop iteration

The method of using loop iteration is one of the most commonly used methods. We can loop through the entire two-dimensional array and use a variable to save the currently found maximum value. If a value greater than the current maximum value is found, the current maximum value is updated to the new value.

The code for this method is as follows:

function findMaxValue($array)
{
    $maxValue = $array[0][0]; // 首先假设最大值为数组首元素
    foreach ($array as $value) {
        foreach ($value as $num) {
            if ($num > $maxValue) { // 发现更大的值,就更新最大值
                $maxValue = $num;
            }
        }
    }
    return $maxValue; // 返回最大值
}
Copy after login

Usage example:

$array = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);

echo findMaxValue($array); // 输出 9
Copy after login

Method 2: Use recursion

Using recursion can handle the second step more easily Dimensional arrays, especially when the array is irregular, can be better solved by recursion.

To use the recursive method, you need to write a recursive function. The job of this recursive function is to call itself recursively if the current element is an array, otherwise return the current element. The result of the recursive function is the maximum value among all elements. Each iteration, we need to compare the size of the current element with the current maximum value and update the maximum value.

The code for this method is as follows:

function findMaxValue($array, $maxValue = null)
{
    if (!is_array($array)) {
        if ($maxValue === null) { // 如果还没有找到最大值
            return $array;
        }
        return max($array, $maxValue); // 和当前最大值比较
    }
    foreach ($array as $value) {
        $maxValue = findMaxValue($value, $maxValue); // 递归调用自身
    }
    return $maxValue; // 返回最大值
}
Copy after login

Usage example:

$array = array(
    array(1, 2, 3),
    array(4, array(5, 9, 6), 7),
    array(10, 11, 12)
);

echo findMaxValue($array); // 输出 12
Copy after login
Copy after login

Method 3: Use PHP built-in function

This method uses PHP’s built-in function max () and call_user_func_array() to find the maximum value in a two-dimensional array.

We will use a feature called "reference", which will be discussed in the following examples.

The code for this method is as follows:

function findMaxValue($array)
{
    return max(call_user_func_array('array_merge', $array)); // 返回最大值
}
Copy after login

Usage example:

$array = array(
    array(1, 2, 3),
    array(4, array(5, 9, 6), 7),
    array(10, 11, 12)
);

echo findMaxValue($array); // 输出 12
Copy after login
Copy after login

This method is relatively simple, but it is not easy to understand. Its principle is as follows:

  • Use array_merge() to convert a two-dimensional array into a one-dimensional array. The array_merge() function we pass to call_user_func_array() merges all the arrays nested in the 2D array into a single array.
  • Use call_user_func_array() to call the max() function and pass it the value in the array.

When the array is long, using this method can improve efficiency because it only requires a single iteration of the elements in the array instead of double iteration.

Conclusion

The above are three methods for finding the maximum value of a two-dimensional array. In different situations, different methods can be used to achieve the best performance. It is recommended to use method one when doing basic development programming, because this method can be well understood and is easy to read and maintain. And if you have a piece of code that needs to find the maximum value of a two-dimensional array with a complex nested structure, you should use a recursive function because it is simpler and clearer. Using PHP's built-in functions, you can get results quickly, especially when a large amount of data needs to be processed, the efficiency is obvious.

The above is the detailed content of Find the maximum value of a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template