bloodykis Posted July 22, 2008 Share Posted July 22, 2008 I have a nested associative array, and i need to sort the children of the last parent by the values of one key. Array ( [0] => Array ( [name] => website.jpg [mod] => 1216300822 [type] => hltv [game] => halo [dir] => demos/halo2/HLTV/ ) [1] => Array ( [name] => island_wars.scm [mod] => 1216300823 [type] => hltv [game] => halo [dir] => demos/halo2/HLTV/ ) [2] => Array ( [name] => preview.jpg [mod] => 1216300828 [type] => hltv [game] => halo [dir] => demos/halo2/HLTV/ ) ) I need all of these to be in descending order by the [mod] key: Array ( [2] => Array ( [name] => preview.jpg [mod] => 1216300828 [type] => hltv [game] => halo [dir] => demos/halo2/HLTV/ ) [1] => Array ( [name] => island_wars.scm [mod] => 1216300823 [type] => hltv [game] => halo [dir] => demos/halo2/HLTV/ ) [0] => Array ( [name] => website.jpg [mod] => 1216300822 [type] => hltv [game] => halo [dir] => demos/halo2/HLTV/ ) ) That is the order it should be in, but I have tried php's built-in sort functions, and tried doing them recursively with no luck. The method has to be close to the speed of a qsort, bubble wont cut it. Any help is greatly appreciated, this has been bugging me for a while now. Quote Link to comment Share on other sites More sharing options...
.josh Posted July 22, 2008 Share Posted July 22, 2008 from a comment of sort() in the manual: http://www.php.net/manual/en/function.sort.php#75381 just slap an array_reverse() around the function call or modify the function to suit your needs. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 22, 2008 Share Posted July 22, 2008 Created right from the example in the manual for array_multisort(): http://us2.php.net/manual/en/function.array-multisort.php Assuming $data_array is your array: <?php // Create an array of just the mod values foreach ($data_array as $key => $row) { $mod[$key] = $row['mod']; } // Sort the data with mod ascending. Add $data_array // as the last parameter, to sort by the common key array_multisort($mod, SORT_ASC, $data_array); ?> Or you could also use usort(): http://us2.php.net/manual/en/function.usort.php <?php function sort_by_mod($a, $b) { if ($a['mod'] == $b['mod']) { return 0; } return ($a['mod'] < $b['mod']) ? -1 : 1; } usort($data_array, 'sort_by_mod'); ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted July 22, 2008 Share Posted July 22, 2008 hey...that's pretty cool, I'll have to remember that one Quote Link to comment Share on other sites More sharing options...
bloodykis Posted July 23, 2008 Author Share Posted July 23, 2008 Created right from the example in the manual for array_multisort(): http://us2.php.net/manual/en/function.array-multisort.php Assuming $data_array is your array: <?php // Create an array of just the mod values foreach ($data_array as $key => $row) { $mod[$key] = $row['mod']; } // Sort the data with mod ascending. Add $data_array // as the last parameter, to sort by the common key array_multisort($mod, SORT_ASC, $data_array); ?> added 2 lines of that to my existing loops and it works like a charm. creative use w/ usort Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.