Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following array of days of the week, with each day having hours of the day (the whole array represents the schedule of a part-time employee):

Array
    (
        [7] => Array
            (
                [0] => 15
                [1] => 14
                [2] => 13
                [3] => 11
                [4] => 12
                [5] => 10
            )

        [1] => Array
            (
                [0] => 10
                [1] => 13
                [2] => 12
            )

        [6] => Array
            (
                [0] => 14
            )

        [3] => Array
            (
                [0] => 4
                [1] => 5
                [2] => 6
            )

    )

and I simply need to:

  1. sort asc each sub-array (2nd dimension) - no need to maintain the numeric keys, values are integers
  2. sort asc the 1st dimension and maintain the numeric, integer keys

ie:

Array
    (
        [1] => Array
            (
                [0] => 10
                [1] => 12
                [2] => 13
            )

        [3] => Array
            (
                [0] => 4
                [1] => 5
                [2] => 6
            )

        [6] => Array
            (
                [0] => 14
            )

        [7] => Array
            (
                [0] => 10
                [1] => 11
                [2] => 12
                [3] => 13
                [4] => 14
                [5] => 15
            )

    )

Additional info:

  1. only the keys of the 1st dimension and the values of the 2nd dimension (and of course their association) are meaningful to my use-case
  2. the 1st dimension can have at most 7 values, ranging from 1-7 (days of the week), and will have at least 1 value (1 day)
  3. the 2nd dimension can have at most 24 values, ranging from 0-23 (hours of each day), and will have at least 1 value (1 hour per day)

I know I can do this with a foreach on the whole ksorted array and sort each 2nd dimension array:

ksort($sched);
foreach ($sched as &$array) sort($array);
unset($array);

but I was hoping I could achieve this with native php array function(s) instead.

My search led me to try array_multisort(array_values($array), array_keys($array), $array) but I just can't make it work.

share|improve this question

3 Answers

up vote -1 down vote accepted

This is the best we can do:

ksort($arr);
foreach ($arr as $k => $v)
  array_multisort($arr[$k]);
share|improve this answer
thanks, @bsdnoobz , 'preciate the answer. – Ana Ban May 24 '12 at 7:17
ok, i don't know what happened to the other answers here, but they're gone! anyways, i chose this one for EXACTLY answering my question, but in the end i just did sort instead of array_multisort in this bit. thanks, @bsdnoobz – Ana Ban May 24 '12 at 9:34
array_multisort with only one array is completely useless, I'd say. It is the same as using sort($arr[$k]) - and thats what the original poster started with. – Sven Oct 13 '12 at 9:27

At the end of the day, you really have TWO sorting tasks to fulfill. The first is to sort the outer array by key. The second is to sort the inner arrays by value. Both sorting is independent from each other.

The best solution is the one you already found. array_multisort() will not help you, because that is made for sorting that actually depends on each other, like "sort by last name - if equal, sort by first name". But it should really be avoided, because defining a sorting function and using usort() and friends much better communicates what you are actually sorting.

share|improve this answer

TRY following line

array_multisort(array_values($array), array_keys($array), ksort($array))

This may raise a warning but you can avoid it by error_reporting(0) if it really resolve the issue.

share|improve this answer
Coding something that needs to avoid warnings should never be done. Fix the problem instead. – Sven Oct 13 '12 at 9:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.