Как настроить пользовательскую сортировку многомерного массива в PHP (кодингитер) - PullRequest
0 голосов
/ 12 февраля 2020

У меня есть массив вроде:

Array
(
    [0] => Array
        (
            [category_id] => 411
            [category_name] => some text
            [data] => Array
                (
                    [1] => stdClass Object
                        (
                            [album_id] => 91689
                            [name] => some text                            
                            [pubDate] => December 23, 2019
                        )

                    [0] => stdClass Object
                        (
                            [album_id] => 91670
                            [name] => some text                           
                            [pubDate] => November 23, 2019
                        )

                )

        )

    [1] => Array
        (
            [category_id] => 15755
            [category_name] => some text
            [data] => Array
                (
                    [1] => stdClass Object
                        (
                            [album_id] => 93624
                            [name] => some text                           
                            [pubDate] => January 02, 2019
                        )

                    [0] => stdClass Object
                        (
                            [album_id] => 93621
                            [name] => some text                            
                            [pubDate] => January 02, 2019
                        )

                )

        )

    [2] => Array
        (
            [category_id] => 15879
            [category_name] => some text
            [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [album_id] => 91683
                            [name] => some text                          
                            [pubDate] => December 31, 2019
                        )

                )

        )

    [3] => Array
        (
            [category_id] => 456
            [category_name] => some text
            [data] => Array
                (
                    [3] => stdClass Object
                        (
                            [album_id] => 93675
                            [name] => some text                           
                            [pubDate] => January 03, 2019
                        )

                    [2] => stdClass Object
                        (
                            [album_id] => 91679
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                    [1] => stdClass Object
                        (
                            [album_id] => 91675
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                    [0] => stdClass Object
                        (
                            [album_id] => 91667
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                )

        )

    [4] => Array
        (
            [category_id] => 435
            [category_name] => some text
            [data] => Array
                (
                    [2] => stdClass Object
                        (
                            [album_id] => 93552
                            [name] => some text
                            [pubDate] => December 31, 2019
                        )

                    [1] => stdClass Object
                        (
                            [album_id] => 91685
                            [name] =>some text
                            [pubDate] => November 23, 2019
                        )

                    [0] => stdClass Object
                        (
                            [album_id] => 91677
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                )

        )

)

, и я хочу вот так:

Array
(
    [0] => Array
        (
            [category_id] => 411
            [category_name] => some text
            [data] => Array
                (
                    [1] => stdClass Object
                        (
                            [album_id] => 91689
                            [name] => some text                            
                            [pubDate] => December 23, 2019
                        )

                    [0] => stdClass Object
                        (
                            [album_id] => 91670
                            [name] => some text                           
                            [pubDate] => November 23, 2019
                        )

                )

        )

    [1] => Array
        (
            [category_id] => 15755
            [category_name] => some text
            [data] => Array
                (
                    [1] => stdClass Object
                        (
                            [album_id] => 93624
                            [name] => some text                           
                            [pubDate] => January 02, 2019
                        )

                    [0] => stdClass Object
                        (
                            [album_id] => 93621
                            [name] => some text                            
                            [pubDate] => January 02, 2019
                        )

                )

        )

    [2] => Array
        (
            [category_id] => 15879
            [category_name] => some text
            [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [album_id] => 91683
                            [name] => some text                          
                            [pubDate] => December 31, 2019
                        )

                )

        )

    [3] => Array
        (
            [category_id] => 456
            [category_name] => some text
            [data] => Array
                (
                    [3] => stdClass Object
                        (
                            [album_id] => 93675
                            [name] => some text                           
                            [pubDate] => January 03, 2019
                        )

                    [2] => stdClass Object
                        (
                            [album_id] => 91679
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                    [1] => stdClass Object
                        (
                            [album_id] => 91667
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                    [0] => stdClass Object
                        (
                            [album_id] => 91675
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                )

        )

    [4] => Array
        (
            [category_id] => 435
            [category_name] => some text
            [data] => Array
                (
                    [2] => stdClass Object
                        (
                            [album_id] => 91685
                            [name] => some text
                            [pubDate] => December 31, 2019
                        )

                    [1] => stdClass Object
                        (
                            [album_id] => 91677
                            [name] =>some text
                            [pubDate] => November 23, 2019
                        )

                    [0] => stdClass Object
                        (
                            [album_id] => 93552
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                )

        )

)

Пожалуйста, помогите! Спасибо!

1 Ответ

0 голосов
/ 12 февраля 2020

Вы можете использовать usort для того же самого для сортировки внутреннего массива,

//convert to array if not,
$arr = json_decode(json_encode($arr), true);
function custom($a, $b)
{
    if(($a["album_id"] == $a["album_id"])) return 0;
    return ($a["album_id"] >= $a["album_id"]) ? -1 : 1; // interchange for order change
}
foreach($arr as &$v){
    uasort($v['data'], "custom");    
}

Демо

Вывод: -

Array
(
    [0] => Array
        (
            [category_id] => 411
            [category_name] => some text
            [data] => Array
                (
                    [0] => Array
                        (
                            [album_id] => 91689
                            [name] => some text                            
                            [pubDate] => December 23, 2019
                        )

                    [1] => Array
                        (
                            [album_id] => 91670
                            [name] => some text                           
                            [pubDate] => November 23, 2019
                        )

                )

        )

    [1] => Array
        (
            [category_id] => 15755
            [category_name] => some text
            [data] => Array
                (
                    [0] => Array
                        (
                            [album_id] => 93624
                            [name] => some text                           
                            [pubDate] => January 02, 2019
                        )

                    [1] => Array
                        (
                            [album_id] => 93621
                            [name] => some text                            
                            [pubDate] => January 02, 2019
                        )

                )

        )

    [2] => Array
        (
            [category_id] => 15879
            [category_name] => some text
            [data] => Array
                (
                    [0] => Array
                        (
                            [album_id] => 91683
                            [name] => some text                          
                            [pubDate] => December 31, 2019
                        )

                )

        )

    [3] => Array
        (
            [category_id] => 456
            [category_name] => some text
            [data] => Array
                (
                    [0] => Array
                        (
                            [album_id] => 93675
                            [name] => some text                           
                            [pubDate] => January 03, 2019
                        )

                    [1] => Array
                        (
                            [album_id] => 91679
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                    [2] => Array
                        (
                            [album_id] => 91675
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                    [3] => Array
                        (
                            [album_id] => 91667
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                )

        )

    [4] => Array
        (
            [category_id] => 435
            [category_name] => some text
            [data] => Array
                (
                    [0] => Array
                        (
                            [album_id] => 93552
                            [name] => some text
                            [pubDate] => December 31, 2019
                        )

                    [1] => Array
                        (
                            [album_id] => 91685
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                    [2] => Array
                        (
                            [album_id] => 91677
                            [name] => some text
                            [pubDate] => November 23, 2019
                        )

                )

        )

)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...