Hi folks,

I have the following data in a table :

G Scale
G 1:29 Scale
HO/O/N Scale
HO/N Scale
HO Scale
O Scale
N Scale
HH Scale
1:500 Scale
1:400 Scale
1:300 Scale
1:250 Scale
1:144 Scale
1:100 Scale
1:72 Scale
1:48 Scale
1:32 Scale
1:24 Scale
1:12 Scale
1:10 Scale

... and I would like it displayed using natsort(). The SQL query retrieves this data as an array with the following format :

Array (
[0] => Array ( [id] => 1 [name] => G Scale )
[1] => Array ( [id] => 2 [name] => G 1:29 Scale )
[2] => Array ( [id] => 3 [name] => HO/O/N Scale )
[3] => Array ( [id] => 4 [name] => HO/N Scale )
[4] => Array ( [id] => 5 [name] => HO Scale )
...and so on...
)

I've been trying to figure this one out... what is the best way to implement natsort() on this multidimensional array, preserving key-value associations? The natural sort should be on the "name" field.

Many thanks!

Gene C
http://www.jobsvolution.ca

    You can use usort() to write your own comparison rule that uses strnatcmp

    (Something like "return strnatcmp($a['name'],$b['name'];");

      Use a custom sort function using [man]strnatcmp[/man] called from [man]uasort[/man]

      function natorder($a,$b) {
         return strnatcmp ( $a['name'], ( $b['name'] );
      }
      
      uasort ($array, 'natorder');
      

      hth

        Write a Reply...