Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've the following multidimensional array:

Array
(
    [0] => Array
        (
            [codes] => Array
                (
                    [2] => 01
                    [1] => 01
                    [0] => 02
                )
            [name] => Test
        )

     [1] => Array
        (
            [codes] => Array
                (
                    [1] => 02
                    [0] => 03
                )
            [name] => Test
        )

     [2] => Array
        (
            [codes] => Array
                (
                    [3] => 01
                    [2] => 01
                    [1] => 01
                    [0] => 02
                )
            [name] => Test
        )
)

I need to sort the array by the codes value, so in this example the expected array would be:

Array
(
    [0] => Array
        (
            [codes] => Array
                (
                    [3] => 01
                    [2] => 01
                    [1] => 01
                    [0] => 02
                )
            [name] => Test
        )

     [1] => Array
        (
            [codes] => Array
                (
                    [2] => 01
                    [1] => 01
                    [0] => 02                        
                )
            [name] => Test
        )

     [2] => Array
        (
            [codes] => Array
                (
                    [1] => 02
                    [0] => 03
                )
            [name] => Test
        )
)

The solution I was thinking is to use the uasort function using strnatcasecmp as a callback, like this:

uasort($array, function($a, $b) {
    return strnatcasecmp($a['codes'], $b['codes']);
});

But it won't work because codes is an array. I don't know how to implement it in this case.

Any idea?

share|improve this question
1  
What is the logic behind the codes array ? How would you compare them to one another ? When you have the answer to this, you will have your code. –  Calimero yesterday
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.