Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按年-月-日排序数组_Php - Fatal编程技术网

Php 按年-月-日排序数组

Php 按年-月-日排序数组,php,Php,我有一个多维的项目数组,都有一个日期。数组的格式如下:$relatedItems[$year][$month][$day][。有没有办法把它按日期排序。我尝试了krsort()bu,我认为这只会对年份键进行排序,例如[2014]。对不起,如果这是模糊的,我是新的php Array ( [2014] => Array ( [01] => Array ( [13] =&

我有一个多维的项目数组,都有一个日期。数组的格式如下:
$relatedItems[$year][$month][$day][
。有没有办法把它按日期排序。我尝试了
krsort()
bu,我认为这只会对年份键进行排序,例如[2014]。对不起,如果这是模糊的,我是新的php

Array
(
    [2014] => Array
        (
            [01] => Array
                (
                    [13] => Array
                        (
                            [0] => Array
                                (
                                    [type] => news-blog
                                    [title] => Jungle News
                                    [date] => 13th January 2014
                                    [url] => /sport/jungle-ultra/news/jungle-news/
                                    [description] => Description goes here
                                )

                        )

                    [23] => Array
                        (
                            [0] => Array
                                (
                                    [type] => gallery
                                    [title] => New Gallery
                                    [url] => /sport/jungle-ultra/galleries/new-gallery/
                                    [images] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [title] => 10.jpg
                                                    [src] => /files/cache/978f98c61f63757334fbeec79fabe482_f65.jpg
                                                )

                                            [1] => Array
                                                (
                                                    [title] => 9.jpg
                                                    [src] => /files/cache/1526727d35b28930b49c69b065a229de_f64.jpg
                                                )

                                            [2] => Array
                                                (
                                                    [title] => 8.jpg
                                                    [src] => /files/cache/d1b67fefed5e7b7644ad9ec1fe8559ae_f63.jpg
                                                )

                                            [3] => Array
                                                (
                                                    [title] => 7.jpg
                                                    [src] => /files/cache/80160e395233a15d1b1df4df67ec565d_f62.jpg
                                                )

                                            [4] => Array
                                                (
                                                    [title] => 6.jpg
                                                    [src] => /files/cache/48575e2d53c487f99633a1105e7a322b_f61.jpg
                                                )

                                            [5] => Array
                                                (
                                                    [title] => 4.jpg
                                                    [src] => /files/cache/9fe01089fc9656562fb7f082499439fc_f60.jpg
                                                )

                                        )

                                )

                        )

                    [27] => Array
                        (
                            [0] => Array
                                (
                                    [type] => video
                                    [title] => Test
                                    [url] => /sport/jungle-ultra/video/test/
                                    [thumb] => /files/5313/8996/9327/speaker-header-small.jpg
                                )

                        )

                    [24] => Array
                        (
                            [0] => Array
                                (
                                    [type] => video
                                    [title] => Rich reaches half way point
                                    [url] => /sport/jungle-ultra/video/rich-reaches-half-way-point/
                                    [thumb] => /files/7013/9039/4602/video-cover-test.jpg
                                )

                        )

                    [21] => Array
                        (
                            [0] => Array
                                (
                                    [type] => audio
                                    [title] => Day 1 - Update from Alexander Island
                                    [url] => /sport/jungle-ultra/audio/day-1-update-alexander-island/
                                    [thumb] => /files/7013/9039/4602/video-cover-test.jpg
                                )

                        )

                )

        )

)

使用
ksort
是正确的,但需要使用递归函数。尝试使用以下方法:

function sort_by_date($dates){
    if(is_array($dates)){ // If it is an array
        ksort($dates); // Sort the array keys
        foreach($dates as $date){ // Loop through the keys
            sort_by_date($date); // And call this recursive function on each of the child arrays
        }
    }
}
用法:

$array = array(...); // Your date array

sort_by_date($array); // New function for sorting

我上面的数组名为
$relatedItems
,我调用了新函数,但什么也没发生?您是否执行了:
按日期排序($relatedItems)
var\u dump()
的输出是什么样子的?