Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维数组上的max()_Php_Multidimensional Array - Fatal编程技术网

Php 多维数组上的max()

Php 多维数组上的max(),php,multidimensional-array,Php,Multidimensional Array,我有一个输入数据数组,我需要找到最大值和最小值。根据用户选择的选项,POST数组可能类似于以下任何一种: [a] => Array ( [0] => 2 ) [a] => Array ( [0] => 2 [1] => 4 [2] => 7 ) [a] => Array ( [0] => 2 [1] => 4 [2

我有一个输入数据数组,我需要找到最大值和最小值。根据用户选择的选项,POST数组可能类似于以下任何一种:

[a] => Array
(
    [0] => 2
)

[a] => Array
    (
        [0] => 2
        [1] => 4
        [2] => 7
    )
[a] => Array
    (
        [0] => 2
        [1] => 4
        [2] => Array
            (
                [0] => 7
            )

    )
当“a”始终是一维数组时,我通过对数组排序并获取最小值和最大值使其工作,但由于我们添加了多维选项,我被卡住了。

我将使用迭代器:


我想你可以自己找出如何执行
array\u min()

这将首先展平数组,然后应用
max()
函数。我曾考虑过展平它,但不想冒覆盖现有密钥的风险。也许我做错了
function array_max($arr) {
  $max = null;

  foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)) as $value) {
    if ($max === null || $value > $max) {
      $max = $value;
    }
  }
  return $max;
}