简体   繁体   中英

find maximum value of a parameter within an array

I have an array in the following format, which I got via the following code:

echo print_r($list);

The array is:

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

What I would like to do is to get the maximum value for the parameter weightvol` within this array.

I have tried:

echo max($list, 'weightvol');

and

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

But something is saying to me that max() isn't the correct function to use in this case. Can anyone help me out here.

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

if you're running PHP >= 5.5.0;

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

for earlier versions of PHP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM