php 或任何程序语言

发布于 2024-10-18 17:38:37 字数 152 浏览 1 评论 0原文

大家好,任何人都可以帮助我找到下面给出的数组的最大值。我预计 650 的结果是最大值......

$my_array = array(array(128,300,140)10,15,array(130,array(500,650))); 

Hi anybody can help me to find the maximum value of the array that are given in the below . i expect the result of 650 is the maximum value....

$my_array = array(array(128,300,140)10,15,array(130,array(500,650))); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

贵在坚持 2024-10-25 17:38:37

在这里,在 3 行可读的代码中使用 RecursiveArrayIterator

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$flattenedArray = iterator_to_array($it);
$max = max($flattenedArray);

或者,如果您不想展平(和复制),而是更喜欢迭代(使用更少的内存,但速度更慢):

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$max = 0;
foreach ($it as $value) {
    $max = max($value, $max);
}

Here you go, using RecursiveArrayIterator in 3 readable lines of code:

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$flattenedArray = iterator_to_array($it);
$max = max($flattenedArray);

Or, if you want to not flatten (and copy), but prefer to iterate (uses far less memory, but slower):

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$max = 0;
foreach ($it as $value) {
    $max = max($value, $max);
}
云雾 2024-10-25 17:38:37

展平数组,然后对其调用 max() 。从您的示例来看,max() 的返回值应为 650。

Flatten the array, then call max() on it. The return value of max() should be 650 from your example.

太阳哥哥 2024-10-25 17:38:37

还可能的是

$data = array(array(128,300,140),10,15,array(130,array(500,650)));
$max = 0;
array_walk_recursive(
    $data,
    function($val) use (&$max) {
        if($val > $max) $max = $val;
    }
);
echo $max; // 650

Also possible is

$data = array(array(128,300,140),10,15,array(130,array(500,650)));
$max = 0;
array_walk_recursive(
    $data,
    function($val) use (&$max) {
        if($val > $max) $max = $val;
    }
);
echo $max; // 650
好倦 2024-10-25 17:38:37

您也可以递归地执行此操作,如果该项目是一个数组,则再次调用该函数以返回该数组中的最大项目。

最后,您应该始终拥有最大项,然后在最后一次迭代中,您可以从这些结果中调用最大值。

You could also do it recursively, if the item is an array, call the function again to return the max item from that array.

In the end you should have always the max item and then in the last iteration, you could call the max from those results.

憧憬巴黎街头的黎明 2024-10-25 17:38:37

这可以解决问题:

function flatten($ar) {
    $toflat = array($ar);
    $res = array();
    while (($r = array_shift($toflat)) !== NULL) {
        foreach ($r as $v) {
            if (is_array($v)) {
                $toflat[] = $v;
            } else {
                $res[] = $v;
            }
        }
    }
    return $res;
}

$arr = array(array(128,300,140),10,15,array(130,array(500,650)));
echo max(array_flatten($arr));

编辑:使用 如何在 PHP 中将多维数组“扁平化”为简单数组?

This does the trick:

function flatten($ar) {
    $toflat = array($ar);
    $res = array();
    while (($r = array_shift($toflat)) !== NULL) {
        foreach ($r as $v) {
            if (is_array($v)) {
                $toflat[] = $v;
            } else {
                $res[] = $v;
            }
        }
    }
    return $res;
}

$arr = array(array(128,300,140),10,15,array(130,array(500,650)));
echo max(array_flatten($arr));

EDIT: Updated flatten array with the one at How to "flatten" a multi-dimensional array to simple one in PHP?

贱贱哒 2024-10-25 17:38:37
<?php
$my_array = array(array(128,300,140),10,15,array(130,array(500,650)));

function findLargest($arr) {
    $largest = 0;
    foreach ($arr as $item) {
        if (is_array($item)) {
            $item = findLargest($item);
        }
        if ($item > $largest) {
            $largest = $item;
        }
    }
    return $largest;
}

echo "Largest is ".findLargest($my_array)."\n";

?>
<?php
$my_array = array(array(128,300,140),10,15,array(130,array(500,650)));

function findLargest($arr) {
    $largest = 0;
    foreach ($arr as $item) {
        if (is_array($item)) {
            $item = findLargest($item);
        }
        if ($item > $largest) {
            $largest = $item;
        }
    }
    return $largest;
}

echo "Largest is ".findLargest($my_array)."\n";

?>
纵性 2024-10-25 17:38:37
function maximum($in)
{
  if (!is_array($in)) $max = $in;
  else foreach ($in as $element) 
  {
    $elementMax = maximum($element);
    if (isset($max)) $max = max($elementMax, $max); else $max = $elementMax;
  }

  return $max;
}
function maximum($in)
{
  if (!is_array($in)) $max = $in;
  else foreach ($in as $element) 
  {
    $elementMax = maximum($element);
    if (isset($max)) $max = max($elementMax, $max); else $max = $elementMax;
  }

  return $max;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文