获取数组列中的最小值和最大值
我有一个这种格式的数组:
$array = [
['id' => 117, 'name' => 'Networking', 'count' => 16],
['id' => 188, 'name' => 'FTP', 'count' => 23],
['id' => 189, 'name' => 'Internet', 'count' => 48],
];
有没有好的方法来检索“计数”列的最小值和最大值(分别为 16
和 48
)?
我可以使用几个循环来做到这一点,但我想知道是否有更好的方法。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
与其他人发布的内容相反,您不能使用
min()< /code>
/
max()
< /a> 解决此问题的函数,因为这些函数不理解传入的数据结构(数组)。这些函数仅适用于标量数组元素。开始编辑
之所以使用min()
和max()
似乎产生正确的答案与将数组类型转换为整数有关,这是一个 未定义行为:我上面关于类型转换的陈述是错误的。实际上
min()
和max()
可以处理数组,但不能在OP 需要它们工作的方式。当使用min()
和max()
具有多个数组或数组数组元素从左到右逐个元素进行比较:翻译成OP的问题,这显示了直接使用
min()
和max()
似乎产生了正确的结果。数组的第一个元素是id
值,因此min()
和max ()
将首先比较它们,顺便得出正确的结果,因为最低的id
是具有最低count
和最高id
的那个。 code>id 是count
最高的那个。END EDIT
正确的方法是使用循环。
In contrast to what others have posted, you cannot use the
min()
/max()
functions for this problem as these functions do not understand the datastructure (array) which are passed in.These functions only work for scalar array elements.BEGIN EDIT
The reason why the use ofmin()
andmax()
seem to yield the correct answer is related to type-casting arrays to integers which is an undefined behaviour:My statement above about the type-casting was wrong. Actually
min()
andmax()
do work with arrays but not in the way the OP needs them to work. When usingmin()
andmax()
with multiple arrays or an array of arrays elements are compared element by element from left to right:Translated into the OP's problem this shows the reason why the direct use of
min()
andmax()
seems to yield the correct result. The arrays' first elements are theid
-values, thereforemin()
andmax()
will compare them first, incidentally resulting in the correct result because the lowestid
is the one with the lowestcount
and the highestid
is the one with the highestcount
.END EDIT
The correct way would be to use a loop.
正如您从前面的答案中看到的,有许多可行的技术需要考虑。
如果您正在考虑最佳性能,那么您应该最小化所使用的循环数量和函数调用数量。
“在幕后”,本机函数
min()
和max()
将完全迭代它们所输入的数组。因此,如果您使用函数或构造来循环二维输入数组,然后调用min()
,然后调用max()
,您将迭代完整的输入数组长度的三次。考虑这个仅迭代 1 次的代码片段:
代码:(演示)
array_shift()
来改变输入数组,因此,如果您在同一范围内重复使用该数组,它将不再包含第一个值另一种技术重视函数式代码。 PHP 有本机函数可以让这项任务井井有条。
array_column()
可以隔离count
列中的所有值。min()
返回最小的数字。max()
返回最大数字。代码:(Demo)
count
元素,它不会生成任何警告As you can see from the earlier answers, there are many viable techniques to consider.
If you are considering best performance, then you should be minimizing the number of loops that are used and the number of function calls.
"Under the hood", the native functions
min()
andmax()
will be fully iterating the array that they are fed. So, if you use a function or construct to loop over the 2-dimensional input array, then callmin()
, then callmax()
, you are iterating the full length of the input array three separate times.Consider this snippet that only iterates 1 time:
Code: (Demo)
array_shift()
, so if you re-use the array in the same scope, it will no longer contain the first valueAnother technique places value on functionally styled code. PHP has native functions that make tidy work of this task.
array_column()
can isolate all of the values in thecount
column.min()
returns the lowest number.max()
returns the highest number.Code: (Demo)
count
element您可以使用 max() 和 min() 函数。
You could use the max() and min() functions.
你用几个循环做了什么?一个就足够了:)
What did you do with a few loops? One is quite enough :)
看起来你不能在二维数组上使用 max() 。它只返回最大的数组,而不是每个索引的 max() (如一些答案中提到的)。
所以:
Looks like you can't use max() on a 2D array. It just returns the largest array, not the max() of each index (as being mentioned in a few answers).
So:
如果所需的列位于数组中的第一个,您可以使用以下单行代码:
这将找到 id 的最小值/最大值
If the desired column is first in the array you can use the following one-liners:
This will find the min/max of id
是否有与该函数等效的内置函数?
(即使没有测试能力)
Is there an equivalent build-in function to that one?
(even without the test capability)