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...

1
  • put 4 spaces in front of each line to format them. Commented Mar 30, 2010 at 7:19

3 Answers 3

7

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Wow, awesome, Thank You VERY MUCH! Why is it a dirty solution though?
@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.
Quick and dirty is often the best.
@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.
Ok thanks for that. This is exactly what I need. Good to know that it needed a pointer (&). I suppose that grabs the key value instead of the actual value? Cheers :)
|
3

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);
}

2 Comments

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: 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.
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.