nash_1126 Posted May 4, 2009 Share Posted May 4, 2009 Hi Im new here... I just want to find out how do i actually re-format or re-sort an array of data (multi-dimensional array). Example of the Array: Array ( [3] => Array ( [0] => 2 [1] => -2 [2] => 2 [3] => 4 ) [4] => Array ( [0] => 0 ) [5] => Array ( [0] => 1 [1] => 0 ) [6] => Array ( [0] => 0 [1] => 0 [2] => 1 ) [7] => Array ( [0] => 1 [1] => 9 ) [11] => Array ( [0] => 0 [1] => 1 ) ) I need the data AS WELL as the Main Keys to be sorted out altogether (without seperating them). I want the results to be like this: (sorted in desc order with keys still intact) [7][1] => 9 [3][3] => 4 [3][0] => 2 [3][2] => 2 .... and so on Quote Link to comment https://forums.phpfreaks.com/topic/156752-help-on-how-to-sort-multi-dimensional-array-with-keys/ Share on other sites More sharing options...
sasa Posted May 4, 2009 Share Posted May 4, 2009 what you try to do someting like this <?php $test = Array( 3 => Array( 0 => 2, 1 => -2, 2 => 2, 3 => 4), 4 => Array( 0 => 0), 5 => Array( 0 => 1, 1 => 0), 6 => Array( 0 => 0, 1 => 0, 2 => 1), 7 => Array( 0 => 1, 1 => 9), 11 => Array( 0 => 0, 1 => 1) ); foreach ($test as $k1 => $v1){ foreach ($v1 as $k2 => $v2){ $tmp[] = array( 'value' => $v2, 'key1' => $k1, 'key2' => $k2); } } rsort($tmp); print_r($tmp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/156752-help-on-how-to-sort-multi-dimensional-array-with-keys/#findComment-825539 Share on other sites More sharing options...
law Posted May 4, 2009 Share Posted May 4, 2009 If he has a really long string of data in those foreach loops wouldn't that kill the computer he is working on? Would adding the & help speed up a long array by making it a pointer instead of a duplicate array? I am asking because I am in a similar situation. foreach ($test as $k1 => &$v1){ foreach ($v1 as $k2 => &$v2){ $tmp[] = array( 'value' => $v2, 'key1' => $k1, 'key2' => $k2); } } Quote Link to comment https://forums.phpfreaks.com/topic/156752-help-on-how-to-sort-multi-dimensional-array-with-keys/#findComment-825659 Share on other sites More sharing options...
lispwriter Posted May 4, 2009 Share Posted May 4, 2009 I don't think it would hurt though I don't think it would be necessary unless you're talkin' about a really massive text file. I don't think I'd go to using references there unless I found that the code was consuming too much memory. Quote Link to comment https://forums.phpfreaks.com/topic/156752-help-on-how-to-sort-multi-dimensional-array-with-keys/#findComment-825785 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.