<?php
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
?>

In this example, we will order by volume descending, edition ascending.

We have an array of rows, but array_multisort() requires an array of
columns, so we use the below code to obtain the columns, then perform the
sorting.

<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>

 

 

Andrew 

 

From: [email protected] [mailto:[email protected]] On Behalf
Of Andrew McMurtrie
Sent: Tuesday, 28 July 2009 9:16 a.m.
To: [email protected]
Subject: [phpug] Re: Sorting Multidimensional Array on values of multiple
keys

 

http://us3.php.net/manual/en/array.sorting.php

 

Andrew 

 

From: [email protected] [mailto:[email protected]] On Behalf
Of Aaron Cooper
Sent: Tuesday, 28 July 2009 9:00 a.m.
To: [email protected]
Subject: [phpug] Sorting Multidimensional Array on values of multiple keys

 

Morning,

 

A challenge.

 

So I have this array, simple example as follows:

 

$testAry = array(
  array('a' => 1, 'b' => 2, 'c' => 3),
  array('a' => 2, 'b' => 1, 'c' => 3),
  array('a' => 3, 'b' => 2, 'c' => 1),
  array('a' => 1, 'b' => 3, 'c' => 2),
  array('a' => 2, 'b' => 3, 'c' => 1),
  array('a' => 3, 'b' => 1, 'c' => 2)
); 

 

And I want to sort this array as in the same fashion as "ORDER BY" in sql
statements. So think of the array as a table with columns. In my particular
usage case, I would like order by 'a' primarily and 'b' secondarily.

 

I imagine the function call will look something like this:
orderBy($results_array, 'a ASC, b DESC');

 

Does anyone have a function to do this? I found a wrapper under usort() docs
but it doesn't seem to work, even with it's own test data.

 

And no, I can't do this at DB level unfortunately. The array in question is
built in various stages.

 

Cheers

Aaron




__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4282 (20090727) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

 



__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4282 (20090727) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4282 (20090727) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

 


--~--~---------~--~----~------------~-------~--~----~
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]
-~----------~----~----~----~------~----~------~--~---

Reply via email to