Page 1 of 1

Sorting assiciative arrays...

Posted: Mon Aug 13, 2007 10:53 pm
by Axet
Alright, well, I have an associative array which assigns arrays of 4 values to strings (see the example below):

Code: Select all

Array ( [value1] => Array ( [0] => 65 [1] => 2 [2] => 75 [3] => -10 ) [value2] => Array ( [0] => 65 [1] => 2 [2] => 0 [3] => 65 ) [value3] => Array ( [0] => 15 [1] => 2 [2] => 5 [3] => 10 ) )
Up to now, I've been using ksort on the big array and it sorted it by the "value1", "value2", etc., alphabetically. Now, however, I want it sorted numerically by the 4th value of each smaller array inside the big array (the -10, 65, and 10 in the above example).

How would I go about doing this?

Thanks in advance for any help :D

Posted: Mon Aug 13, 2007 11:53 pm
by s.dot
Doing that might be pretty tough. Requring a couple loops and using asort() with the SORT_NUMERIC flag.

What are you trying to achieve, perhaps there's an easier way of doing it than sorting by the 4th value of nested associative arrays.

Posted: Tue Aug 14, 2007 4:11 am
by stereofrog
Try usort

Next time when posting example data, please use var_export, not print_r.