Scatless Posted April 14, 2011 Share Posted April 14, 2011 Hello, i need some help with sorting arrays by it's value. lets say i want to have two different html tables listing the employees and another for the managers, then i want to sort them by Group ID's: Etc: Workers as the key / 1 as the value. Groups: Workers => 1 Managers => 2 Workers Group ID John 1 Peter 1 Lance 1 Managers Group ID Carol 2 Greg 2 Brian 2 How can i do this? Link to comment https://forums.phpfreaks.com/topic/233714-array-sorting-by-value/ Share on other sites More sharing options...
monkeytooth Posted April 14, 2011 Share Posted April 14, 2011 ksort http://us.php.net/ksort or asort http://us.php.net/asort i just recently had a similar issue.. and completely forgot about those until someone else reminded me. Link to comment https://forums.phpfreaks.com/topic/233714-array-sorting-by-value/#findComment-1201537 Share on other sites More sharing options...
Cagecrawler Posted April 14, 2011 Share Posted April 14, 2011 asort() can order the elements by their values, but you don't really need to do this. If each table only has one value, then just loop through the array and for each table only print out the people with the relevant group ID. //eg. for workers foreach($people as $person => $group){ if($group == 1) echo $person; } Actual implementation depends on how the data is stored etc. Link to comment https://forums.phpfreaks.com/topic/233714-array-sorting-by-value/#findComment-1201541 Share on other sites More sharing options...
Scatless Posted April 14, 2011 Author Share Posted April 14, 2011 thanks, i will check this out as soon as possible. Link to comment https://forums.phpfreaks.com/topic/233714-array-sorting-by-value/#findComment-1201546 Share on other sites More sharing options...
Scatless Posted April 15, 2011 Author Share Posted April 15, 2011 Hi, i got a little problem here.. i get the array output for every worker 11 times like this John John John Peter Peter Peter how to fix this? Link to comment https://forums.phpfreaks.com/topic/233714-array-sorting-by-value/#findComment-1202002 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.