Page 1 of 1

Find the biggest array value

Posted: Thu Jan 06, 2011 8:25 am
by JKM
Hi there,

I got this array, $data, and I am wondering how I can manage to make php find the biggest key value - which is 1746.[text]Array
(
[2005] => Array
(
[perm] => 317
[temp1] => 0
[temp2] => 93
[temp] => 93
[unba] => 13
[rows] => 292
[jazz] => 0
[news] => 0
[cssp] => 0
[csst] => 0
[cssu] => 0
)

[2006] => Array
(
[perm] => 1292
[temp1] => 16
[temp2] => 300
[temp] => 316
[unba] => 43
[rows] => 1171
[jazz] => 0
[news] => 0
[cssp] => 0
[csst] => 0
[cssu] => 0
)

[2007] => Array
(
[perm] => 450
[temp1] => 2
[temp2] => 879
[temp] => 881
[unba] => 83
[rows] => 460
[jazz] => 0
[news] => 0
[cssp] => 0
[csst] => 0
[cssu] => 0
)

[2008] => Array
(
[perm] => 1150
[temp1] => 2
[temp2] => 1744
[temp] => 1746
[unba] => 206
[rows] => 1046
[jazz] => 0
[news] => 0
[cssp] => 0
[csst] => 0
[cssu] => 0
)

[2009] => Array
(
[perm] => 1338
[temp1] => 1
[temp2] => 1695
[temp] => 1696
[unba] => 86
[rows] => 1272
[jazz] => 346
[news] => 260
[cssp] => 69
[csst] => 55
[cssu] => 1
)

[2010] => Array
(
[perm] => 574
[temp1] => 6
[temp2] => 748
[temp] => 754
[unba] => 35
[rows] => 663
[jazz] => 192
[news] => 122
[cssp] => 88
[csst] => 112
[cssu] => 6
)

[2011] => Array
(
[perm] => 2
[temp1] => 0
[temp2] => 4
[temp] => 4
[unba] => 1
[rows] => 1
[jazz] => 0
[news] => 1
[cssp] => 0
[csst] => 0
[cssu] => 0
)

)
[/text]

Re: Find the biggest array value

Posted: Thu Jan 06, 2011 9:10 am
by anantha
use max() function to find the largest value..see php website to use the max() function

Re: Find the biggest array value

Posted: Thu Jan 06, 2011 10:00 am
by AbraCadaver
Probably a slicker way, but try:

Code: Select all

$max = 0;

foreach($array as $item) {
   $max = (max($item) > $max) ? max($item) : $max;
}

Re: Find the biggest array value

Posted: Fri Jan 07, 2011 12:32 pm
by Weirdan

Code: Select all

$max = ~PHP_INT_MAX;
foreach ($array as $item) {
    $max = max($max, max($item));
}