PHP How to find max value from two dimensional array

0 votes
asked Jul 25, 2014 in Php by anonymous

I have a two dimensional array and I want to find maximum value from of a particular key from the whole array collection.

Here is the array I have and I want to find the maximum value for the Key recCount

[0] => stdClass Object
(
[id] => 1
[name] => 'Old IPhones and Small Androids'
[recCount] => 1
)
[1] => stdClass Object
(
[id] => 2
[name] => 'Low-end Windows Phone'
[recCount] => 1
)
[2] => stdClass Object
(
[id] => 3
[name] => 'IPhone'
[recCount] => 2
)
[3] => stdClass Object
(
[id] => 4
[name] => 'High-end Windows Phone'
[recCount] => 2
)
[4] => stdClass Object
(
[id] => 5
[name] => 'IPad'
[recCount] => 2
)

Share

1 Answer

+1 vote
answered Jul 25, 2014 by Sundar
selected Jul 25, 2014 by administrator
 
Best answer

Simple way to find the max value from two dimensional array is using foreach statement, convert the key field from which we need to find the max value to another single dimensional array and get max value using 'max' function as below

$recCountArray = array();
foreach ($devices as $device) {
   $recCountArray[] = $availableDevice['recCount'];
}

$maxCount = max($recCountArray);

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...