Page 1 of 1

Sorting an array without array_multisort

Posted: Mon Jan 25, 2010 4:05 pm
by iainh
I am used to using array_multisort and a wonderous thing it is too, but I'm seeing a result I simply don't understand and somehow sorting my array without getting as far as array_multisort. So to explain...

I have a nested array so a number of nodes, each with a child array of a number of further nodes. One of these child nodes is a date (in yyyymmdd format), so something like:

Code: Select all

Array
(
    [0] => Array
         (
            [date] => 20100501
            [value2] => South
            [value3] => 7
        )
     [1] => Array
        (
            [date] => 20100424
            [value2] => North
            [value3] => 7
        )
...
And I want to sort in array by date order. Simple...

Code: Select all

foreach($masterArray as $childArray)
{
    $sortByDate[] = $childArray['date'];
}
$success = array_multisort($sortByDate, SORT_ASC, $masterArray);
print_r($masterArray);
So what am I complaining about. Well this...

Code: Select all

foreach($masterArray as $childArray)
{
    $sortByDate[] = $childArray;
}
print_r($sortByDate);
The result I cannot understand is that $sortByDate is $masterArray as if sorted by date through array_multisort, yet I would expect $sortByDate to simply be a clone of $masterArray built up by appending each $childArray defined during the foreach loop reproducing what I'd started with. A useless exercise...

So I am at a loss to see how I end up in the second example with a sorted array. There is no array indexing going on in the foreach loop that would result in the recordering of the array. Quite what am I doing? I cannot see how I am building a sorted array!

Re: Sorting an array without array_multisort

Posted: Mon Jan 25, 2010 5:53 pm
by VladSun
Take a loojk at "Example #3 Sorting database results" in http://php.net/manual/en/function.array-multisort.php

Re: Sorting an array without array_multisort

Posted: Tue Jan 26, 2010 3:32 pm
by iainh
Take a loojk at "Example #3 Sorting database results" in http://php.net/manual/en/function.array-multisort.php
I am clearly missing something. You say look at Example 3 and yes I understand Example 3, but that's not my issue.

If you look at my third example, I am acheiving an array sort without calling array_multisort or any other form of array sort at all, therefore I don't see the relevance of an example showing an array transpose and then sort with array_multisort.

I am trying to fathom out quite how, simply by running through the array and assigning each 'row' (or element) of the master array to a new array ($sortByDate), I somehow end up with a sorted array in $sortByDate. How is this sort occuring? $sortByDate is simply an array made up from each of the child elements of $masterArray assembled in the same order as they are 'picked' from $masterArray by the foreach block and so I would therefore expect it to be a replica of $masterArray, not a sorted version of $masterArray

Re: Sorting an array without array_multisort

Posted: Tue Jan 26, 2010 3:41 pm
by VladSun

Code: Select all

$masterArray = Array
(
    0 => Array
         (
            'date' => 20100501,
            'value2' => South,
            'value3' => 7
        ),
     1 => Array
        (
            'date' => 20100424,
            'value2' => North,
            'value3' => 7
        )
);
 
 
foreach($masterArray as $childArray)
{
    $sortByDate[] = $childArray;
}
print_r($sortByDate);
=>

Code: Select all

Array
(
    [0] => Array
        (
            [date] => 20100501
            [value2] => South
            [value3] => 7
        )
 
    [1] => Array
        (
            [date] => 20100424
            [value2] => North
            [value3] => 7
        )
 
)
I don't think it's sorted.

Re: Sorting an array without array_multisort

Posted: Wed Jan 27, 2010 3:47 pm
by iainh
Well, now I repeat it, no it isn't, I see precisely what I expect to see, a clone of the $masterArray, but believe me, as God is my witness, it was sorted previously which I couldn't fathom...and still can't given the result is no longer consistent...