简体   繁体   English

在多维数组中找到最高值

[英]Find highest value in multi dimensional array

Here is the multi dimensional array 这是多维数组

$arr = array(
    array(141,151,161), 
    2, 
    3, 
    array(
        101, 
        202, 
        array(303,404)
    )
);

How to find the highest value, which the result in the array above should be 404. 如何找到最高值,上面数组中的结果应为404。
The array depth can may be more than 3. 阵列深度可以大于3。


Here is the code i last tried, but i notice this only can check until 3 depth, i need it can be check unlimited depth 这是我上次尝试的代码,但我注意到这只能检查到3深度,我需要它可以检查无限深度

function MaxArray($arr){
    foreach($arr as $valueDepth1){
        if(is_array($valueDepth1)){
            foreach($valueDepth1 as $valueDepth2){
                if(is_array($valueDepth2)){
                    foreach($valueDepth2 as $valueDepth3){
                        $checingArray[]=$valueDepth3;
                    }
                }else{
                    $checingArray[]=$valueDepth2;
                }               
            }
        }else{
            $checingArray[]=$valueDepth1;
        }
    }

    return max($checingArray);
}
function highest($array) {
   foreach($array as $key => $value) {
       if (is_array($value)) {
           $array[$key] = highest($value);
       }
   }

   sort($array);

   return array_pop($array);
}

You can see it in action here: http://codepad.org/4xPFsU1U 你可以在这里看到它: http//codepad.org/4xPFsU1U

You could try something like this: 你可以尝试这样的事情:

$max = -1;
array_walk_recursive($arr, 'arrmax');
function arrmax($item, $key) {
   global $max;
   $max = ($key > $max) ? $key : $max;
}
echo "Maximum number is : " . $max;

Here's a function I found on the first page on google. 这是我在谷歌第一页上找到的功能。 Search engines is a perfect place to find stuff ;) 搜索引擎是寻找东西的理想场所;)

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;
}

Source 资源

This can be a good starting point : 这可以是一个很好的起点

getMax($array)
{
   if ( is_array($array) )
   {
      // NOTE: PHP_MIN doesn't exist. Is just to let you understand the logic
      $max = PHP_MIN; 
      foreach($array as $value)
      {
         if ( is_array($value) )
            $value = getMax($value);

         if ($value > $max)
            $max = $value;
      }
      return $max;
   }
   else
      return $array
}

Try This 尝试这个

function MaxRecursive($arr,$maxVal = null)
{

    try {

        if(! isset($arr) || ! is_array($arr) )
            throw new Exception("The array is empty");

        for($i=0;$i<count($arr);$i++)
        {
            if(is_array($arr[$i]))
                $maxVal =    MaxRecursive($arr[$i],$maxVal);

            else
            {
                if($maxVal == null)
                 $maxVal = $arr[$i];

                if($arr[$i] > $maxVal )
                    $maxVal =  $arr[$i];
            }

        }

        return $maxVal;

    }
    catch (Exception $e)
    {
        echo $e->getMessage();

    }

}
function MaxArray2($arr)
{
    return   MaxRecursive($arr,null);

}
getMax($arr); 
function getMax($arr) { 
    foreach($arr as $arrr) { 
        echo max($arrr[2]); 
    }
}

You have to use recursion, with a single loop. 你必须使用递归,只需一个循环。 For each element, if it is a number, we compare it with the current highest value, if it is an array we call the function recursively to check the array: 对于每个元素,如果它是一个数字,我们将它与当前最高值进行比较,如果它是一个数组,我们递归调用该函数来检查数组:

    function getHeighestNumber($array = array()) {
        static $number  = 0;
        foreach ($array AS $key=>$value) {
            if(is_array($value)) {                    
                $number  = getHeighestNumber($value);
            } else if($value > $number) {
                $number  = $value;
            }                
        }
        return $number;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM