Page 1 of 1

Sorting array question

Posted: Thu Sep 25, 2008 12:17 pm
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! :)

Re: Sorting array question

Posted: Thu Sep 25, 2008 1:12 pm
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

Re: Sorting array question

Posted: Thu Sep 25, 2008 1:37 pm
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