-1

I'm looking for the best(fastest) way to sort multi-dimentional array like this:

array(
        2010 => array(
            1 => array(
                26 => 'some value',
                29 => 'some value',
                31 => 'some value'
            ),
            7 => array(
                13 => 'some value',
                25 => 'some value',
                4 => 'some value'
            )
        ),
        2011 => array(
            12 => array(
                5 => 'some value',
                3 => 'some value',
                19 => 'some value'
            ),
            2 => array(
                7 => 'some value',
                30 => 'some value',
                14 => 'some value'
            )
        )
    )

As you can see, I'm using years, months and days as keys, and I need to sort array by this keys. I will really appreciate any help.

Thanks

2 Answers2

3

You could try this. One could make it more general using recrusive function, but I think this is good enough in your case.

foreach ($years as &$months){
  foreach ($months as &$days){
    ksort($days);
  }
  ksort($months);
}
ksort($years);
luttkens
  • 1,272
  • 8
  • 16
  • Thank you, looks like this is what I was looking for. Using recursive function is good to, but, as I can see, it don't gives any advantage in performance. – Dmitry Gordienko Jul 21 '13 at 14:38
2

You can try "array_walk_recursive" to recursively access the item in multidimensional array to reduce function calls. An example:

function custom_sort(&$val, $key) {
    if (is_array($val)) {
        ksort($val);
    }
}

array_walk_recurisive($arrInput, 'custom_sort');
TroyCheng
  • 571
  • 3
  • 10