查找多维数组中的最小值/最大值

发布于 2024-10-22 23:23:08 字数 785 浏览 5 评论 0原文

我需要在 PHP 中找到多维数组中的最小值和最大值,我有我认为可以在下面工作的内容,但它一直给我一个解析错误,这是家庭作业,我不要求任何人为我做这件事,但我是一个初学者和任何帮助将不胜感激。

<?php

/* 2 dimensional array in PHP - strictly an array of arrays */

$multable[] = array("11", "12", "15", "22", "41", "42");  
$multable[] = array("6", "7", "16", "17", "22", "23");  
$multable[] = array("1", "15", "16", "20", "22", "3");  

<table>
<?php
/* display a table from a 2D array */
for ($j=0;$j<3;$j++) {
    print "<tr>";
    for ($k=0;$k<6;$k++) {
        echo "<td>",$multable[$j][$k],"</td>";
    }
    print "</tr>";
    $max_value = 0;
    foreach ($multable as $myMax) {
        if ($max_value<$myMax) {
            $max_value = $myMax;
        }
    }
echo $max_value;
?>
</table>

I need to find the minimum and maximum in a multidimensional array in PHP, I have what I thought would work below but it keeps giving me a parse error, this is homework and I am not asking anyone to do it for me but I am a beginner and any help would be appreciated.

<?php

/* 2 dimensional array in PHP - strictly an array of arrays */

$multable[] = array("11", "12", "15", "22", "41", "42");  
$multable[] = array("6", "7", "16", "17", "22", "23");  
$multable[] = array("1", "15", "16", "20", "22", "3");  

<table>
<?php
/* display a table from a 2D array */
for ($j=0;$j<3;$j++) {
    print "<tr>";
    for ($k=0;$k<6;$k++) {
        echo "<td>",$multable[$j][$k],"</td>";
    }
    print "</tr>";
    $max_value = 0;
    foreach ($multable as $myMax) {
        if ($max_value<$myMax) {
            $max_value = $myMax;
        }
    }
echo $max_value;
?>
</table>

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

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

发布评论

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

评论(7

看轻我的陪伴 2024-10-29 23:23:08

还有一个单行线:

$max = max( array_map("max", $multable) );

There is also a one-liner for that:

$max = max( array_map("max", $multable) );
落叶缤纷 2024-10-29 23:23:08

使用 php 的 max()min() 函数。

use max() and min() functions of php.

无言温柔 2024-10-29 23:23:08

最大值:

<?php
$multable = array();
$multable[] = array("11", "12", "15", "22", "41", "42");  
$multable[] = array("6", "7", "16", "17", "22", "23");  
$multable[] = array("1", "15", "16", "20", "22", "3");  
$max = -99999999;
foreach($multable as $sub){

  $tempMax = max($sub);

  if($tempMax > $max){
    $max = $tempMax;
  }

}

echo $max;

?>

你可以算出最小值:)

Max:

<?php
$multable = array();
$multable[] = array("11", "12", "15", "22", "41", "42");  
$multable[] = array("6", "7", "16", "17", "22", "23");  
$multable[] = array("1", "15", "16", "20", "22", "3");  
$max = -99999999;
foreach($multable as $sub){

  $tempMax = max($sub);

  if($tempMax > $max){
    $max = $tempMax;
  }

}

echo $max;

?>

You can figure out min :)

秋意浓 2024-10-29 23:23:08

您的 foreach 迭代仅执行一维 - 每个 $myMax 是六个元素列表之一,而不是单个标量值。这就是为什么你的比较不起作用并且条件永远不会成立,你试图将标量与数组进行比较。您所说的 $myMax 更合适地称为 $currentRow

这没关系,因为 PHP 有一些函数可以查找数组的最小值和最大值

http://us.php.net/manual/en/function.min.php
http://us.php.net/manual/en/function.max.php

$max_value = 0; $min_value = $multable[0][0];
foreach ($multable as $currentRow) 
{
    // COMPARE CURRENT ROW's MIN/MAX TO MIN/MAX_VALUE 
    // AND MAKE NEW ASSIGNMENT IF APPROPRIATE
}

或者将其交给老师,看看老师怎么说:

function fComp ($f) {return function ($a,$b) use ($f) {return $f($a, $f($b));};}

$max = array_reduce($multable, fComp('max'), $multable[0][0]);
$min = array_reduce($multable, fComp('min'), $multable[0][0]);

echo "max: $max <br />";
echo "min: $min";

PS - 在制作 HTML 表格的早期迭代中,丢失常量是一种很好的形式。使用 count 来获取数组的长度 - 或者更好 - 使用 foreach 就像稍后所做的那样。 (即使使用 foreach 你仍然需要其中两个嵌套,它不会逐个元素地迭代二维数组)

Your foreach iteration only does one dimension - each $myMax is one of your six element lists and not an individual scalar value. That's why your comparison doesn't work and the conditional is never true, you are trying to compare a scalar with an array. What you call $myMax would more appropriately be called $currentRow

This is ok because PHP has some functions to find the min and max of an array

http://us.php.net/manual/en/function.min.php
http://us.php.net/manual/en/function.max.php

$max_value = 0; $min_value = $multable[0][0];
foreach ($multable as $currentRow) 
{
    // COMPARE CURRENT ROW's MIN/MAX TO MIN/MAX_VALUE 
    // AND MAKE NEW ASSIGNMENT IF APPROPRIATE
}

Or hand this in and see what your teacher says:

function fComp ($f) {return function ($a,$b) use ($f) {return $f($a, $f($b));};}

$max = array_reduce($multable, fComp('max'), $multable[0][0]);
$min = array_reduce($multable, fComp('min'), $multable[0][0]);

echo "max: $max <br />";
echo "min: $min";

PS - in your earlier iterations to make the HTML table, it would be good form to lose the constants. Use count to get the length of the array instead - or better yet - use foreach like you do later on. (Even with foreach you would still need two of them nested, it doesn't iterate a 2-dimensional array element-by-element)

私藏温柔 2024-10-29 23:23:08

对于最小值

echo min(array_map("min", $multable));

对于最大值

echo max(array_map("max", $multable));

For Minimum value

echo min(array_map("min", $multable));

For Maximum Value

echo max(array_map("max", $multable));
各空 2024-10-29 23:23:08
$minArray = array();
        foreach($arrayVal as $arrI=> $arrK)
        {

            if($arrK == min($arrayVal ) )
            {
                array_push($minArray , $arrayVal );
            }
        }

print_r($minArray);

干得好 :)

$minArray = array();
        foreach($arrayVal as $arrI=> $arrK)
        {

            if($arrK == min($arrayVal ) )
            {
                array_push($minArray , $arrayVal );
            }
        }

print_r($minArray);

Here you go :)

栖迟 2024-10-29 23:23:08

我不建议在每个子数组上调用 min()max(),然后在缩减后的数组上再次调用该函数。这会拨打太多电话,而且效率不高。

相反,只需使用扩展和合并技术将索引数组展平一次,然后对展平的数组调用一次 min()max() 即可。

代码:(演示)

$flat = array_merge(...$multable);

printf(
    'Min: %d, Max: %d',
    min($flat),
    max($flat)
);

输出:

Min: 1, Max: 42

如果您只需要一个或另一个结果,那么就不必担心临时变量。

echo min(array_merge(...$multable));

或者

echo max(array_merge(...$multable));

I do not recommend calling min() or max() on each subarray, then calling the function again on the reduced array. This is making too many calls and won't be most efficient.

Instead, flatten the indexed array just once with a spread&merge technique, then call min() or max() just once on the flattened array.

Code: (Demo)

$flat = array_merge(...$multable);

printf(
    'Min: %d, Max: %d',
    min($flat),
    max($flat)
);

Output:

Min: 1, Max: 42

If you only need one or the other outcome, then don't bother with the temporary variable.

echo min(array_merge(...$multable));

Or

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