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? Quote 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. Quote 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! Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.