我想在php中对这个json数组进行排序,但我无法弄清楚如何去做。 我想要最新一季和最新一集1,最新一季最新第1集2等等。
$episodelist = json_decode(file_get_contents('cache/episodes.json'), true);
$episodes = $episodelist["doctor who (2005)"];
json数组:
"doctor who (2005)": {
"1": {
"link": "https:\/\/eztv-proxy.net\/\/ep\/58259\/doctor-who-extra-s01e03-robot-of-sherwood-pdtv-x264-deadpool\/",
"name": "Doctor Who Extra S01E03 Robot Of Sherwood PDTV x264-DEADPOOL (124.73 MB)",
"season": "1",
"episode": "3",
"magnet": "magnet:?xt=urn:btih:DZ7H4EWP2XW6YRYIXEIPMULETLASGDAF&dn=Doctor.Who.Extra.S01E03.Robot.Of.Sherwood.PDTV.x264-DEADPOOL&tr=udp:\/\/tracker.openbittorrent.com:80&tr=udp:\/\/tracker.publicbt.com:80&tr=udp:\/\/tracker.istole.it:80&tr=udp:\/\/open.demonii.com:80&tr=udp:\/\/tracker.coppersurfer.tk:80"
},
"2": {
"link": "https:\/\/eztv-proxy.net\/\/ep\/58254\/doctor-who-2005-8x03-robot-of-sherwood-720p-hdtv-x264-fov\/",
"name": "Doctor Who 2005 8x03 Robot Of Sherwood 720p HDTV x264-FoV (1.02 GB)",
"season": "8",
"episode": "3",
"magnet": "magnet:?xt=urn:btih:S25AOAFO5ZUGXMZ52ZI2OUD5S5TD4ZBI&dn=Doctor.Who.2005.8x03.Robot.Of.Sherwood.720p.HDTV.x264-FoV&tr=udp:\/\/tracker.openbittorrent.com:80&tr=udp:\/\/tracker.publicbt.com:80&tr=udp:\/\/tracker.istole.it:80&tr=udp:\/\/open.demonii.com:80&tr=udp:\/\/tracker.coppersurfer.tk:80"
},
"3": {
"link": "https:\/\/eztv-proxy.net\/\/ep\/58252\/doctor-who-extra-s01e02-into-the-dalek-720p-webrip-x264-c4tv\/",
"name": "Doctor Who Extra S01E02 Into The Dalek 720p Webrip x264-C4TV (203.70 MB)",
"season": "1",
"episode": "2",
"magnet": "magnet:?xt=urn:btih:FQ6CNHRGQSRKFWIX63JGG4KWLVVOZJQX&dn=Doctor.Who.Extra.S01E02.Into.The.Dalek.720p.Webrip.x264-C4TV&tr=udp:\/\/tracker.openbittorrent.com:80&tr=udp:\/\/tracker.publicbt.com:80&tr=udp:\/\/tracker.istole.it:80&tr=udp:\/\/open.demonii.com:80&tr=udp:\/\/tracker.coppersurfer.tk:80"
},
"4": {
"link": "https:\/\/eztv-proxy.net\/\/ep\/58251\/doctor-who-extra-s01e02-into-the-dalek-webrip-x264-c4tv\/",
"name": "Doctor Who Extra S01E02 Into The Dalek Webrip x264-C4TV (81.13 MB)",
"season": "1",
"episode": "2",
"magnet": "magnet:?xt=urn:btih:OZ7O2O3FE7RADBGOAJEE4UMBROPO7CW7&dn=Doctor.Who.Extra.S01E02.Into.The.Dalek.Webrip.x264-C4TV&tr=udp:\/\/tracker.openbittorrent.com:80&tr=udp:\/\/tracker.publicbt.com:80&tr=udp:\/\/tracker.istole.it:80&tr=udp:\/\/open.demonii.com:80&tr=udp:\/\/tracker.coppersurfer.tk:80"
},
"5": {
"link": "https:\/\/eztv-proxy.net\/\/ep\/58248\/doctor-who-2005-s08e03-hdtv-x264-tla\/",
"name": "Doctor Who 2005 S08E03 HDTV x264-TLA (399.11 MB)",
"season": "8",
"episode": "3",
"magnet": "magnet:?xt=urn:btih:SYDVQP4NMZJ5MQ2CEIOPZWUBLQWNKKHO&dn=Doctor.Who.2005.S08E03.HDTV.x264-TLA&tr=udp:\/\/tracker.openbittorrent.com:80&tr=udp:\/\/tracker.publicbt.com:80&tr=udp:\/\/tracker.istole.it:80&tr=udp:\/\/open.demonii.com:80&tr=udp:\/\/tracker.coppersurfer.tk:80"
}
我希望它变成的数组:
"doctor who (2005)": {
"1": {
"link": "https:\/\/eztv-proxy.net\/\/ep\/58248\/doctor-who-2005-s08e03-hdtv-x264-tla\/",
"name": "Doctor Who 2005 S08E03 HDTV x264-TLA (399.11 MB)",
"season": "8",
"episode": "3",
"magnet": "magnet:?xt=urn:btih:SYDVQP4NMZJ5MQ2CEIOPZWUBLQWNKKHO&dn=Doctor.Who.2005.S08E03.HDTV.x264-TLA&tr=udp:\/\/tracker.openbittorrent.com:80&tr=udp:\/\/tracker.publicbt.com:80&tr=udp:\/\/tracker.istole.it:80&tr=udp:\/\/open.demonii.com:80&tr=udp:\/\/tracker.coppersurfer.tk:80"
},
"2": {
"link": "https:\/\/eztv-proxy.net\/\/ep\/58254\/doctor-who-2005-8x03-robot-of-sherwood-720p-hdtv-x264-fov\/",
"name": "Doctor Who 2005 8x03 Robot Of Sherwood 720p HDTV x264-FoV (1.02 GB)",
"season": "8",
"episode": "3",
"magnet": "magnet:?xt=urn:btih:S25AOAFO5ZUGXMZ52ZI2OUD5S5TD4ZBI&dn=Doctor.Who.2005.8x03.Robot.Of.Sherwood.720p.HDTV.x264-FoV&tr=udp:\/\/tracker.openbittorrent.com:80&tr=udp:\/\/tracker.publicbt.com:80&tr=udp:\/\/tracker.istole.it:80&tr=udp:\/\/open.demonii.com:80&tr=udp:\/\/tracker.coppersurfer.tk:80"
},
"3": {
"link": "https:\/\/eztv-proxy.net\/\/ep\/58259\/doctor-who-extra-s01e03-robot-of-sherwood-pdtv-x264-deadpool\/",
"name": "Doctor Who Extra S01E03 Robot Of Sherwood PDTV x264-DEADPOOL (124.73 MB)",
"season": "1",
"episode": "3",
"magnet": "magnet:?xt=urn:btih:DZ7H4EWP2XW6YRYIXEIPMULETLASGDAF&dn=Doctor.Who.Extra.S01E03.Robot.Of.Sherwood.PDTV.x264-DEADPOOL&tr=udp:\/\/tracker.openbittorrent.com:80&tr=udp:\/\/tracker.publicbt.com:80&tr=udp:\/\/tracker.istole.it:80&tr=udp:\/\/open.demonii.com:80&tr=udp:\/\/tracker.coppersurfer.tk:80"
},
"4": {
"link": "https:\/\/eztv-proxy.net\/\/ep\/58251\/doctor-who-extra-s01e02-into-the-dalek-webrip-x264-c4tv\/",
"name": "Doctor Who Extra S01E02 Into The Dalek Webrip x264-C4TV (81.13 MB)",
"season": "1",
"episode": "2",
"magnet": "magnet:?xt=urn:btih:OZ7O2O3FE7RADBGOAJEE4UMBROPO7CW7&dn=Doctor.Who.Extra.S01E02.Into.The.Dalek.Webrip.x264-C4TV&tr=udp:\/\/tracker.openbittorrent.com:80&tr=udp:\/\/tracker.publicbt.com:80&tr=udp:\/\/tracker.istole.it:80&tr=udp:\/\/open.demonii.com:80&tr=udp:\/\/tracker.coppersurfer.tk:80"
},
"5": {
"link": "https:\/\/eztv-proxy.net\/\/ep\/58252\/doctor-who-extra-s01e02-into-the-dalek-720p-webrip-x264-c4tv\/",
"name": "Doctor Who Extra S01E02 Into The Dalek 720p Webrip x264-C4TV (203.70 MB)",
"season": "1",
"episode": "2",
"magnet": "magnet:?xt=urn:btih:FQ6CNHRGQSRKFWIX63JGG4KWLVVOZJQX&dn=Doctor.Who.Extra.S01E02.Into.The.Dalek.720p.Webrip.x264-C4TV&tr=udp:\/\/tracker.openbittorrent.com:80&tr=udp:\/\/tracker.publicbt.com:80&tr=udp:\/\/tracker.istole.it:80&tr=udp:\/\/open.demonii.com:80&tr=udp:\/\/tracker.coppersurfer.tk:80"
}
答案 0 :(得分:1)
您希望通过usort()
实施自定义排序。有点像这样:
$episodelist = json_decode(file_get_contents('cache/episodes.json'), true);
foreach ($episodelist as &$show) {
usort($show, function ($a, $b) {
if ($a["season"] == $b["season"]) {
if ($a["episode"] == $b["episode"]) {
return 0;
}
return ($a["episode"] > $b["episode"]) ? -1 : 1;
}
return ($a["season"] > $b["season"]) ? -1 : 1;
}
);
}
var_export($episodelist);
array (
'doctor who (2005)' =>
array (
0 =>
array (
'link' => 'https://eztv-proxy.net//ep/58248/doctor-who-2005-s08e03-hdtv-x264-tla/',
'name' => 'Doctor Who 2005 S08E03 HDTV x264-TLA (399.11 MB)',
'season' => '8',
'episode' => '3',
'magnet' => 'magnet:?xt=urn:btih:SYDVQP4NMZJ5MQ2CEIOPZWUBLQWNKKHO&dn=Doctor.Who.2005.S08E03.HDTV.x264-TLA&tr=udp://tracker.openbittorrent.com:80&tr=udp://tracker.publicbt.com:80&tr=udp://tracker.istole.it:80&tr=udp://open.demonii.com:80&tr=udp://tracker.coppersurfer.tk:80',
),
1 =>
array (
'link' => 'https://eztv-proxy.net//ep/58254/doctor-who-2005-8x03-robot-of-sherwood-720p-hdtv-x264-fov/',
'name' => 'Doctor Who 2005 8x03 Robot Of Sherwood 720p HDTV x264-FoV (1.02 GB)',
'season' => '8',
'episode' => '3',
'magnet' => 'magnet:?xt=urn:btih:S25AOAFO5ZUGXMZ52ZI2OUD5S5TD4ZBI&dn=Doctor.Who.2005.8x03.Robot.Of.Sherwood.720p.HDTV.x264-FoV&tr=udp://tracker.openbittorrent.com:80&tr=udp://tracker.publicbt.com:80&tr=udp://tracker.istole.it:80&tr=udp://open.demonii.com:80&tr=udp://tracker.coppersurfer.tk:80',
),
2 =>
array (
'link' => 'https://eztv-proxy.net//ep/58259/doctor-who-extra-s01e03-robot-of-sherwood-pdtv-x264-deadpool/',
'name' => 'Doctor Who Extra S01E03 Robot Of Sherwood PDTV x264-DEADPOOL (124.73 MB)',
'season' => '1',
'episode' => '3',
'magnet' => 'magnet:?xt=urn:btih:DZ7H4EWP2XW6YRYIXEIPMULETLASGDAF&dn=Doctor.Who.Extra.S01E03.Robot.Of.Sherwood.PDTV.x264-DEADPOOL&tr=udp://tracker.openbittorrent.com:80&tr=udp://tracker.publicbt.com:80&tr=udp://tracker.istole.it:80&tr=udp://open.demonii.com:80&tr=udp://tracker.coppersurfer.tk:80',
),
3 =>
array (
'link' => 'https://eztv-proxy.net//ep/58252/doctor-who-extra-s01e02-into-the-dalek-720p-webrip-x264-c4tv/',
'name' => 'Doctor Who Extra S01E02 Into The Dalek 720p Webrip x264-C4TV (203.70 MB)',
'season' => '1',
'episode' => '2',
'magnet' => 'magnet:?xt=urn:btih:FQ6CNHRGQSRKFWIX63JGG4KWLVVOZJQX&dn=Doctor.Who.Extra.S01E02.Into.The.Dalek.720p.Webrip.x264-C4TV&tr=udp://tracker.openbittorrent.com:80&tr=udp://tracker.publicbt.com:80&tr=udp://tracker.istole.it:80&tr=udp://open.demonii.com:80&tr=udp://tracker.coppersurfer.tk:80',
),
4 =>
array (
'link' => 'https://eztv-proxy.net//ep/58251/doctor-who-extra-s01e02-into-the-dalek-webrip-x264-c4tv/',
'name' => 'Doctor Who Extra S01E02 Into The Dalek Webrip x264-C4TV (81.13 MB)',
'season' => '1',
'episode' => '2',
'magnet' => 'magnet:?xt=urn:btih:OZ7O2O3FE7RADBGOAJEE4UMBROPO7CW7&dn=Doctor.Who.Extra.S01E02.Into.The.Dalek.Webrip.x264-C4TV&tr=udp://tracker.openbittorrent.com:80&tr=udp://tracker.publicbt.com:80&tr=udp://tracker.istole.it:80&tr=udp://open.demonii.com:80&tr=udp://tracker.coppersurfer.tk:80',
),
),
)
答案 1 :(得分:0)
我认为您希望usort()
具有以下comperator函数:
usort($array, function($a, $b){
return $a['season'] * 1000 + $a['episode'] < $b['season'] * 1000 + $b['episode'] ? 1 : -1;
});
usort
函数使用提供的函数作为比较器来决定哪个元素是第一个。这样它就可以命令所有元素。我不确定你是否必须扭转它。如果是这种情况,只需将<
替换为>
即可。我使用因子1000
使季节参数比剧集参数更强。我假设每个赛季的节目数不会超过999集。
当然,您需要使用json_decode
将json字符串转换为php数组。
添加内联if语句以使其正常工作(?1:-1)