Sorting array question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aidave
Forum Newbie
Posts: 2
Joined: Thu Sep 25, 2008 12:13 pm

Sorting array question

Post by aidave »

Hi, hopefully this question is easy.

I have a big array, made like this:

Code: Select all

 
$row['name'] = "employee";
$row['etc1'] = "value1";
$row['date'] = "date";
$row['etc2'] = "value3";
$row['etc3'] = "value4";
$rows[] = $row;
// keeping adding new $row to $rows
 
How would I then sort this $rows array based on, for example:

- Sort by "name" and then by "date"?


Thank you for your time! :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting array question

Post by VladSun »

The first question to ask - it seems that your data comes from a DB, so why dont you use ORDER BY in your SQL query.

http://bg.php.net/function.array-multisort
Example #3 Sorting database results
There are 10 types of people in this world, those who understand binary and those who don't
aidave
Forum Newbie
Posts: 2
Joined: Thu Sep 25, 2008 12:13 pm

Re: Sorting array question

Post by aidave »

Thanks for the reply.

Unfortunately the data is not from SQL, i suppose it could be dumped into temp sql table then sorted back, but seems redundant...

Its working now!

Code: Select all

 
    foreach ($rows as $key => $row) {
        $employees[$key]  = $row['employee_id'];
        $workDates[$key] = $row['work_day'];
        //echo $key . " - " . $workDates[$key] . "<BR>";
    }
    
array_multisort($employees, SORT_ASC, $workDates, SORT_ASC, $rows);
 
thank you
Post Reply