Welcome Guest, Not a member yet? Register   Sign In
Sorting Multidimensional Arrays
#1

[eluser]Fielder[/eluser]
How do I sort both levels of this array, by their keys? I want the years to be in order, and the 1,2,3,4 keys to be in order (regardless of their respective values). I've tried sort, ksort, arsort, natsort... and various foreach() calls.
The array is $monthData.
Code:
Array
(
    [2011] => Array
        (
            [3] => 40
            [1] => 0
            [2] => 0
            [4] => 0
        )

    [2012] => Array
        (
            [3] => 45
            [1] => 0
            [2] => 0
            [4] => 0
        )

    [2010] => Array
        (
            [1] => 51
            [2] => 0
            [3] => 0
            [4] => 0
        )

)
#2

[eluser]Dyllon[/eluser]
How exactly did ksort() fail you?
#3

[eluser]mah0001[/eluser]
Code:
ksort($monthData);//this won't sort the sub-arrays

//iterate sub-arrays and sort
foreach($monthData as $key=>$value)
{
    ksort($monthData[$key]);
}
#4

[eluser]Fielder[/eluser]
Thanks mah0001... worked - figured I was close to begin with.

So this worked..
Code:
foreach ($monthData as $key => $val)
            {
                ksort($monthData[$key]);
            }

//print_r(ksort($monthData)); <- didn't work, output was "1" only.

ksort($monthData);

print_r($monthData);




Theme © iAndrew 2016 - Forum software by © MyBB