Find the biggest array value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Find the biggest array value

Post 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]
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: Find the biggest array value

Post by anantha »

use max() function to find the largest value..see php website to use the max() function
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Find the biggest array value

Post by AbraCadaver »

Probably a slicker way, but try:

Code: Select all

$max = 0;

foreach($array as $item) {
   $max = (max($item) > $max) ? max($item) : $max;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Find the biggest array value

Post by Weirdan »

Code: Select all

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