在php中找到特定多维数组值的max() [英] find max() of specific multidimensional array value in php
本文介绍了在php中找到特定多维数组值的max()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如查看数组:此处
See array for example: here
基本上,我想在该数组中找到 array[]['time'] 的 max().如果我遍历它,我可以弄清楚,但我希望有一个更优雅的解决方案.
Basically, I want to find the max() for array[]['time'] in that array. I can figure it out if I loop through it, but I was hoping there was a more elegant solution.
推荐答案
如果您想在数组上迭代计算某个值,请考虑 array_reduce
:
Think array_reduce
if you want to compute some single value iteratively over an array:
$max = array_reduce($array, function($a, $b) {
return $a > $b['time'] ? $a : $b['time'];
} );
或者你可以使用一个实用函数,例如:
Or you could make use of a utility function like:
function array_col(array $a, $x)
{
return array_map(function($a) use ($x) { return $a[$x]; }, $a);
}
这将被用作:
$max = max(array_col($array, 'time'));
它更通用,可以用于其他类似的事情.
It's more general purpose and could be used for other similar things.
这篇关于在php中找到特定多维数组值的max()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文