按日期(键)和时间(值)对多维数组进行排序
Sort multi-dimensional array by date (keys) and time (values)
假设下面的巨大数组(我故意没有删除它的任何记录,这样你就有了大量的数据,以防有人想深入挖掘并帮助我找到我的问题的解决方案):
$program = array (
'2021-07-11' =>
array (
0 =>
array (
'movie' => 'Movie 1',
'channel' => 'Channel 10',
'time' => '23:00',
),
1 =>
array (
'movie' => 'Movie 2',
'channel' => 'Channel 2',
'time' => '01:15',
),
2 =>
array (
'movie' => 'Movie 8',
'channel' => 'Channel 4',
'time' => '11:00',
),
3 =>
array (
'movie' => 'Movie 12',
'channel' => 'Channel 11',
'time' => '17:30',
),
4 =>
array (
'movie' => 'Movie 14',
'channel' => 'Channel 5',
'time' => '21:00',
),
5 =>
array (
'movie' => 'Movie 15',
'channel' => 'Channel 5',
'time' => '02:20',
),
6 =>
array (
'movie' => 'Movie 17',
'channel' => 'Channel 11',
'time' => '17:30',
),
7 =>
array (
'movie' => 'Movie 19',
'channel' => 'Channel 5',
'time' => '10:00',
),
8 =>
array (
'movie' => 'Movie 21',
'channel' => 'Channel 8',
'time' => '1:20',
),
9 =>
array (
'movie' => 'Movie 33',
'channel' => 'Channel 5',
'time' => '00:20',
),
10 =>
array (
'movie' => 'Movie 39',
'channel' => 'Channel 3',
'time' => '17:30',
),
11 =>
array (
'movie' => 'Movie 40',
'channel' => 'Channel 3',
'time' => '22:00',
),
12 =>
array (
'movie' => 'Movie 41',
'channel' => 'Channel 5',
'time' => '14:00',
),
13 =>
array (
'movie' => 'Movie 42',
'channel' => 'Channel 5',
'time' => '23:00',
),
14 =>
array (
'movie' => 'Movie 43',
'channel' => 'Channel 2',
'time' => '14:15',
),
15 =>
array (
'movie' => 'Movie 44',
'channel' => 'Channel 2',
'time' => '18:00',
),
16 =>
array (
'movie' => 'Movie 45',
'channel' => 'Channel 2',
'time' => '23:45',
),
17 =>
array (
'movie' => 'Movie 46',
'channel' => 'Channel 1',
'time' => '22:00',
),
18 =>
array (
'movie' => 'Movie 47',
'channel' => 'Channel 6',
'time' => '21:10',
),
),
'2021-07-10' =>
array (
0 =>
array (
'movie' => 'Movie 3',
'channel' => 'Channel 2',
'time' => '14:15',
),
1 =>
array (
'movie' => 'Movie 5',
'channel' => 'Channel 3',
'time' => '22:00',
),
2 =>
array (
'movie' => 'Movie 6',
'channel' => 'Channel 11',
'time' => '23:00',
),
3 =>
array (
'movie' => 'Movie 7',
'channel' => 'Channel 4',
'time' => '11:15',
),
4 =>
array (
'movie' => 'Movie 11',
'channel' => 'Channel 4',
'time' => '21:00',
),
5 =>
array (
'movie' => 'Movie 16',
'channel' => 'Channel 2',
'time' => '23:45',
),
6 =>
array (
'movie' => 'Movie 20',
'channel' => 'Channel 4',
'time' => '23:00',
),
7 =>
array (
'movie' => 'Movie 23',
'channel' => 'Channel 5',
'time' => '01:00',
),
8 =>
array (
'movie' => 'Movie 27',
'channel' => 'Channel 4',
'time' => '01:00',
),
9 =>
array (
'movie' => 'Movie 29',
'channel' => 'Channel 7',
'time' => '02:15',
),
10 =>
array (
'movie' => 'Movie 31',
'channel' => 'Channel 5',
'time' => '14:00',
),
11 =>
array (
'movie' => 'Movie 32',
'channel' => 'Channel 5',
'time' => '22:10',
),
12 =>
array (
'movie' => 'Movie 36',
'channel' => 'Channel 2',
'time' => '00:01',
),
13 =>
array (
'movie' => 'Movie 37',
'channel' => 'Channel 2',
'time' => '11:30',
),
14 =>
array (
'movie' => 'Movie 38',
'channel' => 'Channel 6',
'time' => '22:30',
),
),
'2021-07-09' =>
array (
0 =>
array (
'movie' => 'Movie 4',
'channel' => 'Channel 5',
'time' => '21:00',
),
1 =>
array (
'movie' => 'Movie 9',
'channel' => 'Channel 11',
'time' => '22:00',
),
2 =>
array (
'movie' => 'Movie 10',
'channel' => 'Channel 9',
'time' => '16:45',
),
3 =>
array (
'movie' => 'Movie 18',
'channel' => 'Channel 7',
'time' => '22:00',
),
4 =>
array (
'movie' => 'Movie 22',
'channel' => 'Channel 5',
'time' => '23:00',
),
5 =>
array (
'movie' => 'Movie 24',
'channel' => 'Channel 9',
'time' => '21:00',
),
6 =>
array (
'movie' => 'Movie 25',
'channel' => 'Channel 9',
'time' => '23:00',
),
7 =>
array (
'movie' => 'Movie 26',
'channel' => 'Channel 4',
'time' => '21:00',
),
8 =>
array (
'movie' => 'Movie 28',
'channel' => 'Channel 8',
'time' => '23:00',
),
9 =>
array (
'movie' => 'Movie 30',
'channel' => 'Channel 6',
'time' => '23:45',
),
10 =>
array (
'movie' => 'Movie 34',
'channel' => 'Channel 3',
'time' => '23:00',
),
11 =>
array (
'movie' => 'Movie 35',
'channel' => 'Channel 2',
'time' => '22:00',
),
),
'2021-07-12' =>
array (
0 =>
array (
'movie' => 'Movie 13',
'channel' => 'Channel 5',
'time' => '01:20',
),
),
)
我想按日期(第一维的键)和第三维的列 time
对这个数组进行排序,这样我就得到了一个按日期分组的电影列表,并按播放时间排序...
第一个任务是超级简单的一行...
array_multisort(array_map('strtotime', array_keys($program)), SORT_ASC, $program);
然而,第二个任务让我很难以类似的优雅方式实现...我尝试的是:
array_multisort(array_map('strtotime', array_keys($program)), SORT_ASC, array_column($program, 'time'), SORT_ASC, $program);
但由于某些原因这不起作用 array_column($program, 'time')
returns 一个空数组。在 manual 中明确指出数组可以是 一个多维数组或一个对象数组,从中提取一列值。 所以它可以是一个多维,不是严格意义上的二维数组...为什么它在我的情况下不起作用?我错过了什么?
我想到的是多维数组不能是关联数组,所以为了暂时去掉日期键,我把上面改成了:
array_multisort(array_map('strtotime', array_keys($program)), SORT_ASC, array_column(array_values($program), 'time'), SORT_ASC, $program);
但不幸的是这也不起作用...
我正在努力以 smart/elegant 的方式做到这一点并避免使用循环遍历每个值,但我 运行 没有想法。因此,我们将不胜感激任何帮助。 TIA.
好的,我找到了!!!我需要遍历初始数组的每个子数组(通过引用),并在其中执行 array_multisort
...所以下面的代码成功了,我想它已经尽可能优雅了! !!
array_multisort(array_map('strtotime', array_keys($program)), SORT_ASC, $program);
foreach ($program as &$day) {
array_multisort(array_column($day, 'time'), SORT_ASC, $day);
}
假设下面的巨大数组(我故意没有删除它的任何记录,这样你就有了大量的数据,以防有人想深入挖掘并帮助我找到我的问题的解决方案):
$program = array (
'2021-07-11' =>
array (
0 =>
array (
'movie' => 'Movie 1',
'channel' => 'Channel 10',
'time' => '23:00',
),
1 =>
array (
'movie' => 'Movie 2',
'channel' => 'Channel 2',
'time' => '01:15',
),
2 =>
array (
'movie' => 'Movie 8',
'channel' => 'Channel 4',
'time' => '11:00',
),
3 =>
array (
'movie' => 'Movie 12',
'channel' => 'Channel 11',
'time' => '17:30',
),
4 =>
array (
'movie' => 'Movie 14',
'channel' => 'Channel 5',
'time' => '21:00',
),
5 =>
array (
'movie' => 'Movie 15',
'channel' => 'Channel 5',
'time' => '02:20',
),
6 =>
array (
'movie' => 'Movie 17',
'channel' => 'Channel 11',
'time' => '17:30',
),
7 =>
array (
'movie' => 'Movie 19',
'channel' => 'Channel 5',
'time' => '10:00',
),
8 =>
array (
'movie' => 'Movie 21',
'channel' => 'Channel 8',
'time' => '1:20',
),
9 =>
array (
'movie' => 'Movie 33',
'channel' => 'Channel 5',
'time' => '00:20',
),
10 =>
array (
'movie' => 'Movie 39',
'channel' => 'Channel 3',
'time' => '17:30',
),
11 =>
array (
'movie' => 'Movie 40',
'channel' => 'Channel 3',
'time' => '22:00',
),
12 =>
array (
'movie' => 'Movie 41',
'channel' => 'Channel 5',
'time' => '14:00',
),
13 =>
array (
'movie' => 'Movie 42',
'channel' => 'Channel 5',
'time' => '23:00',
),
14 =>
array (
'movie' => 'Movie 43',
'channel' => 'Channel 2',
'time' => '14:15',
),
15 =>
array (
'movie' => 'Movie 44',
'channel' => 'Channel 2',
'time' => '18:00',
),
16 =>
array (
'movie' => 'Movie 45',
'channel' => 'Channel 2',
'time' => '23:45',
),
17 =>
array (
'movie' => 'Movie 46',
'channel' => 'Channel 1',
'time' => '22:00',
),
18 =>
array (
'movie' => 'Movie 47',
'channel' => 'Channel 6',
'time' => '21:10',
),
),
'2021-07-10' =>
array (
0 =>
array (
'movie' => 'Movie 3',
'channel' => 'Channel 2',
'time' => '14:15',
),
1 =>
array (
'movie' => 'Movie 5',
'channel' => 'Channel 3',
'time' => '22:00',
),
2 =>
array (
'movie' => 'Movie 6',
'channel' => 'Channel 11',
'time' => '23:00',
),
3 =>
array (
'movie' => 'Movie 7',
'channel' => 'Channel 4',
'time' => '11:15',
),
4 =>
array (
'movie' => 'Movie 11',
'channel' => 'Channel 4',
'time' => '21:00',
),
5 =>
array (
'movie' => 'Movie 16',
'channel' => 'Channel 2',
'time' => '23:45',
),
6 =>
array (
'movie' => 'Movie 20',
'channel' => 'Channel 4',
'time' => '23:00',
),
7 =>
array (
'movie' => 'Movie 23',
'channel' => 'Channel 5',
'time' => '01:00',
),
8 =>
array (
'movie' => 'Movie 27',
'channel' => 'Channel 4',
'time' => '01:00',
),
9 =>
array (
'movie' => 'Movie 29',
'channel' => 'Channel 7',
'time' => '02:15',
),
10 =>
array (
'movie' => 'Movie 31',
'channel' => 'Channel 5',
'time' => '14:00',
),
11 =>
array (
'movie' => 'Movie 32',
'channel' => 'Channel 5',
'time' => '22:10',
),
12 =>
array (
'movie' => 'Movie 36',
'channel' => 'Channel 2',
'time' => '00:01',
),
13 =>
array (
'movie' => 'Movie 37',
'channel' => 'Channel 2',
'time' => '11:30',
),
14 =>
array (
'movie' => 'Movie 38',
'channel' => 'Channel 6',
'time' => '22:30',
),
),
'2021-07-09' =>
array (
0 =>
array (
'movie' => 'Movie 4',
'channel' => 'Channel 5',
'time' => '21:00',
),
1 =>
array (
'movie' => 'Movie 9',
'channel' => 'Channel 11',
'time' => '22:00',
),
2 =>
array (
'movie' => 'Movie 10',
'channel' => 'Channel 9',
'time' => '16:45',
),
3 =>
array (
'movie' => 'Movie 18',
'channel' => 'Channel 7',
'time' => '22:00',
),
4 =>
array (
'movie' => 'Movie 22',
'channel' => 'Channel 5',
'time' => '23:00',
),
5 =>
array (
'movie' => 'Movie 24',
'channel' => 'Channel 9',
'time' => '21:00',
),
6 =>
array (
'movie' => 'Movie 25',
'channel' => 'Channel 9',
'time' => '23:00',
),
7 =>
array (
'movie' => 'Movie 26',
'channel' => 'Channel 4',
'time' => '21:00',
),
8 =>
array (
'movie' => 'Movie 28',
'channel' => 'Channel 8',
'time' => '23:00',
),
9 =>
array (
'movie' => 'Movie 30',
'channel' => 'Channel 6',
'time' => '23:45',
),
10 =>
array (
'movie' => 'Movie 34',
'channel' => 'Channel 3',
'time' => '23:00',
),
11 =>
array (
'movie' => 'Movie 35',
'channel' => 'Channel 2',
'time' => '22:00',
),
),
'2021-07-12' =>
array (
0 =>
array (
'movie' => 'Movie 13',
'channel' => 'Channel 5',
'time' => '01:20',
),
),
)
我想按日期(第一维的键)和第三维的列 time
对这个数组进行排序,这样我就得到了一个按日期分组的电影列表,并按播放时间排序...
第一个任务是超级简单的一行...
array_multisort(array_map('strtotime', array_keys($program)), SORT_ASC, $program);
然而,第二个任务让我很难以类似的优雅方式实现...我尝试的是:
array_multisort(array_map('strtotime', array_keys($program)), SORT_ASC, array_column($program, 'time'), SORT_ASC, $program);
但由于某些原因这不起作用 array_column($program, 'time')
returns 一个空数组。在 manual 中明确指出数组可以是 一个多维数组或一个对象数组,从中提取一列值。 所以它可以是一个多维,不是严格意义上的二维数组...为什么它在我的情况下不起作用?我错过了什么?
我想到的是多维数组不能是关联数组,所以为了暂时去掉日期键,我把上面改成了:
array_multisort(array_map('strtotime', array_keys($program)), SORT_ASC, array_column(array_values($program), 'time'), SORT_ASC, $program);
但不幸的是这也不起作用...
我正在努力以 smart/elegant 的方式做到这一点并避免使用循环遍历每个值,但我 运行 没有想法。因此,我们将不胜感激任何帮助。 TIA.
好的,我找到了!!!我需要遍历初始数组的每个子数组(通过引用),并在其中执行 array_multisort
...所以下面的代码成功了,我想它已经尽可能优雅了! !!
array_multisort(array_map('strtotime', array_keys($program)), SORT_ASC, $program);
foreach ($program as &$day) {
array_multisort(array_column($day, 'time'), SORT_ASC, $day);
}