查找多维数组中的最小值/最大值
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
还有一个单行线:
There is also a one-liner for that:
使用 php 的
max()
和min()
函数。use
max()
andmin()
functions of php.最大值:
你可以算出最小值:)
Max:
You can figure out min :)
您的
foreach
迭代仅执行一维 - 每个$myMax
是六个元素列表之一,而不是单个标量值。这就是为什么你的比较不起作用并且条件永远不会成立,你试图将标量与数组进行比较。您所说的$myMax
更合适地称为$currentRow
这没关系,因为 PHP 有一些函数可以查找数组的最小值和最大值
http://us.php.net/manual/en/function.min.php
http://us.php.net/manual/en/function.max.php
或者将其交给老师,看看老师怎么说:
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
Or hand this in and see what your teacher says:
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 - useforeach
like you do later on. (Even withforeach
you would still need two of them nested, it doesn't iterate a 2-dimensional array element-by-element)对于最小值
对于最大值
For Minimum value
For Maximum Value
干得好 :)
Here you go :)
我不建议在每个子数组上调用
min()
或max()
,然后在缩减后的数组上再次调用该函数。这会拨打太多电话,而且效率不高。相反,只需使用扩展和合并技术将索引数组展平一次,然后对展平的数组调用一次
min()
或max()
即可。代码:(演示)
输出:
如果您只需要一个或另一个结果,那么就不必担心临时变量。
或者
I do not recommend calling
min()
ormax()
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()
ormax()
just once on the flattened array.Code: (Demo)
Output:
If you only need one or the other outcome, then don't bother with the temporary variable.
Or