JSHINER Posted September 3, 2008 Share Posted September 3, 2008 I have an array that is displayed using: foreach($page['array'] as $c) { echo 'some stuff'; } Each has an id .. $c['id'] ... how can I find the highest ID in that array and on that echo have an if statement... so for example the highest ID would read "some stuff HIGHEST". Quote Link to comment https://forums.phpfreaks.com/topic/122617-solved-finding-highest-id-in-an-array/ Share on other sites More sharing options...
.josh Posted September 3, 2008 Share Posted September 3, 2008 max() Quote Link to comment https://forums.phpfreaks.com/topic/122617-solved-finding-highest-id-in-an-array/#findComment-633121 Share on other sites More sharing options...
JSHINER Posted September 3, 2008 Author Share Posted September 3, 2008 I tried that it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/122617-solved-finding-highest-id-in-an-array/#findComment-633125 Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 $maxId = 0; foreach($page['array'] as $c) { $maxId = max($maxId,$c['id']) } foreach($page['array'] as $c) { echo 'some stuff'; if ($c['id'] == $maxId) { echo "Highest ID"; } } I don't like it, as it's looping the array twice, but can't think of anything else right now. Perhaps array_walk_recursive() ?? Quote Link to comment https://forums.phpfreaks.com/topic/122617-solved-finding-highest-id-in-an-array/#findComment-633140 Share on other sites More sharing options...
Adam Posted September 3, 2008 Share Posted September 3, 2008 Could try something like: $highestID = 0; foreach($page['array'] as $c) { if ($c['id'] > $highestID) $highestID = $c['id']; } foreach($page['array'] as $c) { if ($c['id'] == $highestID) { echo 'some stuff'; } else { echo 'some other stuff'; } } It's not that efficient as it needs two loops, I dare say there's another way but.. I dont know it... Adam Quote Link to comment https://forums.phpfreaks.com/topic/122617-solved-finding-highest-id-in-an-array/#findComment-633145 Share on other sites More sharing options...
Ken2k7 Posted September 4, 2008 Share Posted September 4, 2008 What about: $maxId = 0; $results = array(); foreach($page['array'] as $c) { $id = $c['id']; if ($maxId < $id) { $maxId = $id; $results = array(); } if ($maxId == $id) $results[] = $c; } echo "Highest id is: " . $maxId; The $results array would have all the results that has the highest ID in case you wanted those too. You can definitely loop through the $results array as well. Quote Link to comment https://forums.phpfreaks.com/topic/122617-solved-finding-highest-id-in-an-array/#findComment-633302 Share on other sites More sharing options...
.josh Posted September 4, 2008 Share Posted September 4, 2008 foreach($page['array'] as $c) { $ids[] = $c['id']; } echo max($ids); Quote Link to comment https://forums.phpfreaks.com/topic/122617-solved-finding-highest-id-in-an-array/#findComment-633327 Share on other sites More sharing options...
tibberous Posted September 4, 2008 Share Posted September 4, 2008 function crap($a, $b){ return $a['id'] > $b['id']; } usort($page['array'], 'crap'); That work? Quote Link to comment https://forums.phpfreaks.com/topic/122617-solved-finding-highest-id-in-an-array/#findComment-633333 Share on other sites More sharing options...
JSHINER Posted September 4, 2008 Author Share Posted September 4, 2008 thanks guys! Figured it out. Quote Link to comment https://forums.phpfreaks.com/topic/122617-solved-finding-highest-id-in-an-array/#findComment-633590 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.