找到数组中参数的最大值

时间:2015-03-17 14:39:50

标签: php arrays

我有一个以下格式的数组,我通过以下代码获得:

echo print_r($list);

数组是:

Array ( 

[0] => Array ( [id] => 105 [id_parent] => 1 [title] => Pellets [id_product] => 129 [product_name] => AKZ Premium EnPlus A1 Pellets [manufacturer] => AKZ [id_location] => 105 [id_category] => 5 [unitmeasurment] => kg [weightvol] => 15 )

[1] => Array ( [id] => 102 [id_parent] => 1 [title] => Firewood [id_product] => 114 [product_name] => Eucalyptus Crate [manufacturer] => Dalby Firewood [id_location] => 102 [id_category] => 2 [unitmeasurment] => m3 [weightvol] => 1.8 ) 

[2] => Array ( [id] => 102 [id_parent] => 1 [title] => Firewood [id_product] => 118 [product_name] => Eucalyptus Bulk Bag [manufacturer] => Dalby Firewood [id_location] => 102 [id_category] => 2 [unitmeasurment] => m3 [weightvol] => 0.75 ) 

)  1

我想要做的是在此数组中获取参数weightvol`的最大值。

我试过了:

echo max($list, 'weightvol');

echo max($list['weightvol']);

但有些事情告诉我max()在这种情况下不是正确的功能。任何人都可以帮助我。

1 个答案:

答案 0 :(得分:2)

echo max(array_column($list, 'weightvol')); 

如果你正在运行PHP> = 5.5.0;

echo max(array_map(
    function($value) {
        return $value['weightvol']; 
    },
    $list
)); 

用于早期版本的PHP