K
K
KuznetsovIS2014-07-07 16:52:48
PHP
KuznetsovIS, 2014-07-07 16:52:48

PHP multidimensional array sorting. nested arrays. How to sort nested arrays by two parameters?

Good afternoon!
Help with sorting.
I have an array like:

Array
  (
    [SOME_RANDOM_INTEGER_VALUE_1] => Array
        (
            [ID] => SOME_RANDOM_INTEGER_VALUE_1
            [SORTING_INFO_ALL_STARS] => Array
                (
                    [0] => 2
                    [1] => 90
                )
    )
    [SOME_RANDOM_INTEGER_VALUE_2] => Array
        (
            [ID] => SOME_RANDOM_INTEGER_VALUE_2
            [SORTING_INFO_ALL_STARS] => Array
                (
                    [0] => 2
                    [1] => 45
                )
    )
    [SOME_RANDOM_INTEGER_VALUE_3] => Array
        (
            [ID] => SOME_RANDOM_INTEGER_VALUE_3
            [SORTING_INFO_ALL_STARS] => Array
                (
                    [0] => 3
                    [1] => 2
                )
    )
    [SOME_RANDOM_INTEGER_VALUE_4] => Array
        (
            [ID] => SOME_RANDOM_INTEGER_VALUE_3
            [SORTING_INFO_ALL_STARS] => Array
                (
                    [0] => 3
                    [1] => 1
                )
    )
    [SOME_RANDOM_INTEGER_VALUE_5] => Array
        (
            [ID] => SOME_RANDOM_INTEGER_VALUE_3
            [SORTING_INFO_ALL_STARS] => Array
                (
                    [0] => 1
                    [1] => 0
                )
    )
)

And I need to convert it to the following:
Array
  (
    [SOME_RANDOM_INTEGER_VALUE_3] => Array
        (
            [ID] => SOME_RANDOM_INTEGER_VALUE_3
            [SORTING_INFO_ALL_STARS] => Array
                (
                    [0] => 3
                    [1] => 2
                )
    )
    [SOME_RANDOM_INTEGER_VALUE_4] => Array
        (
            [ID] => SOME_RANDOM_INTEGER_VALUE_3
            [SORTING_INFO_ALL_STARS] => Array
                (
                    [0] => 3
                    [1] => 1
                )
    )
    [SOME_RANDOM_INTEGER_VALUE_1] => Array
        (
            [ID] => SOME_RANDOM_INTEGER_VALUE_1
            [SORTING_INFO_ALL_STARS] => Array
                (
                    [0] => 2
                    [1] => 90
                )
    )
    [SOME_RANDOM_INTEGER_VALUE_2] => Array
        (
            [ID] => SOME_RANDOM_INTEGER_VALUE_2
            [SORTING_INFO_ALL_STARS] => Array
                (
                    [0] => 2
                    [1] => 45
                )
    )
    [SOME_RANDOM_INTEGER_VALUE_5] => Array
        (
            [ID] => SOME_RANDOM_INTEGER_VALUE_3
            [SORTING_INFO_ALL_STARS] => Array
                (
                    [0] => 1
                    [1] => 0
                )
    )
)

SOME_RANDOM_INTEGER_VALUE_* is the position ID, it can take almost any value.
I'm trying to sort arrays by two parameters:
1) [SOME_RANDOM_INTEGER_VALUE_*] [SORTING_INFO_ALL_STARS][0] - the value of the first sort (from larger to smaller);
2) [SOME_RANDOM_INTEGER_VALUE_*] [SORTING_INFO_ALL_STARS][1 ] - from larger to smaller, but it is necessary to arrange them in a common array, depending on [SORTING_INFO_ALL_STARS][0]
That is, for example, like this:
[3][3] , [3][2] , [2][3] , [2][0] , [1][4] , [1][3] , [1][2] , etc.
Tried to figure it out with usort and array-multisortunfortunately unsuccessful. I can’t figure out how to get the value of [SORTING_INFO_ALL_STARS], bypassing [SOME_RANDOM_INTEGER_VALUE_*], but at the same time move [SOME_RANDOM_INTEGER_VALUE_*] higher or lower ...
As for me, this sorting is quite specific and I would not want to create a separate array with the element ID and value properties and sort this array...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-07-07
Protko @Fesor

Do I understand correctly that you need something like this?:

usort($array, function ($a, $b) {
    return strcmp(
        implode($a['SORTING_INFO_ALL_STARS']), 
        implode($b['SORTING_INFO_ALL_STARS'])
    );
});

K
KuznetsovIS, 2014-07-14
@KuznetsovIS

Yes, as I understand it, this is the optimal solution to the problem.
I'll work with the function that you wrote, and if possible, I'll throw off the working version here, in case someone else encounters the same problem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question