views:

103

answers:

3

I need to sort an array that can look like this:

$array[4][0] = array('id' => 1, 'value' => 2);
$array[3][2] = array('id' => 0, 'value' => 3);
$array[4][1] = array('id' => 1, 'value' => 0);
$array[1][3] = array('id' => 2, 'value' => 1);
$array[1][1] = array('id' => 3, 'value' => 0);
$array[3][0] = array('id' => 2, 'value' => 1);
$array[3][1] = array('id' => 1, 'value' => 0);
$array[1][2] = array('id' => 3, 'value' => 2);
$array[1][0] = array('id' => 2, 'value' => 1);
$array[2][1] = array('id' => 0, 'value' => 2);
$array[2][4] = array('id' => 3, 'value' => 1);

But needs to be sorted and returned as this:

$array[1][0] = array('id' => 2, 'value' => 1);
$array[1][1] = array('id' => 3, 'value' => 0);
$array[1][2] = array('id' => 3, 'value' => 2);
$array[1][3] = array('id' => 2, 'value' => 1);
$array[2][1] = array('id' => 0, 'value' => 2);
$array[2][4] = array('id' => 3, 'value' => 1);
$array[3][0] = array('id' => 2, 'value' => 1);
$array[3][1] = array('id' => 1, 'value' => 0);
$array[3][2] = array('id' => 0, 'value' => 3);
$array[4][0] = array('id' => 1, 'value' => 2);
$array[4][1] = array('id' => 1, 'value' => 0);

Can anyone help me? It needs to sort both indexes of the array from lowest to highest index value. Sounds simple enough, but I'm having the hardest time trying to figure this out, while still keeping the values intact.

Please help someone...

+2  A: 

You want to sort it by key then, and not by values: http://se.php.net/manual/en/function.ksort.php or http://se.php.net/manual/en/function.uksort.php

Edit, Example;

function sorter(array &$multidimensional) {
    foreach ($multidimensional as &$current) {
        if (is_array($current))
            sorter($current);
    }
    ksort($multidimensional);
}
Björn
Can you give me an example please? I'm confused here. I have an array that returns both dimensions. How can I do both keys at the same time?
SoLoGHoST
@sologhost: It'S not done at the same time. First, in the `foreach` loop, **every element/child/entry** of the current array is checked whether it is an array or not, if so, it is sorted. At the end the "main array" is sorted.
Felix Kling
+1  A: 

Something like this should do it:

function ksort_recursive(&$arr) {
    foreach($arr as $key => &$value) {
        if(is_array($value)) {
            ksort_recursive($value);
        }
    } unset($value);
    ksort($arr);
}

ksort_recursive($array);
Tatu Ulmanen
+2  A: 

A quick'n'dirty solution might look something like:

// Sort the outer array
ksort($array); 
// Sort each inner array
foreach($array as &$innerArray)
{
    ksort($innerArray);
}
PatrikAkerstrand
Wow, awesome, Thank You VERY MUCH! Why is it a dirty solution though?
SoLoGHoST
@sologhost: Probably because it is very specialized code. The others wrote a function that can be reused to sort arrays with arbitrary dimensions by keys.
Felix Kling
Quick and dirty is often the best.
Evert
@sologhost: Because, like @Felix said, it's specialized for your specific case, i.e. a two-dimensional array with numeric keys that are to be sorted in ascending order.
PatrikAkerstrand
SoLoGHoST