发现在PHP特定多维数组值MAX() [英] find max() of specific multidimensional array value in php

查看:302
本文介绍了发现在PHP特定多维数组值MAX()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅例如数组: 这里

See array for example: here

基本上,我想找到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);
}

这将使用这样的:

which would be used like:

$max = max(array_col($array, 'time'));

它更通用,并可以用于其它类似的东西。

It's more general purpose and could be used for other similar things.

这篇关于发现在PHP特定多维数组值MAX()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆