Jump to content

Array sorting by value


Scatless

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.