Anti-Moronic Posted April 19, 2012 Share Posted April 19, 2012 I have this array: Array ( [jan] => 2 [feb] => 1 [mar] => 2 [apr] => 1 ) ..and I want to return: Array ('jan', 'mar') As in, find the 2 elements with the highest count and put them in an array. What is the simplest way to achieve this? Link to comment https://forums.phpfreaks.com/topic/261266-get-key-of-arrays-based-on-max/ Share on other sites More sharing options...
salathe Posted April 19, 2012 Share Posted April 19, 2012 Easy, using array_keys. array_keys($array, max($array)) That's assuming you want all of the keys for the single highest value rather than the keys for the two highest items if sorted. Link to comment https://forums.phpfreaks.com/topic/261266-get-key-of-arrays-based-on-max/#findComment-1338859 Share on other sites More sharing options...
Anti-Moronic Posted April 19, 2012 Author Share Posted April 19, 2012 DOH. Thanks. Really should learn my array functions! Link to comment https://forums.phpfreaks.com/topic/261266-get-key-of-arrays-based-on-max/#findComment-1338865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.