Sorting assiciative arrays...

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
Axet
Forum Newbie
Posts: 1
Joined: Mon Aug 13, 2007 10:47 pm

Sorting assiciative arrays...

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Try usort

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