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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.