嘿所有我想知道最高投票数在这个数组中是什么,然后从那里得到 file_path 。
images =>
backdrops =>
0 =>
file_path => /gM3KKiN80qbJgKHjPnmAfwxSicG.jpg
width => 1920
height => 1080
iso_639_1 =>
aspect_ratio => 1.78
vote_average => 5.4529616724739
vote_count => 19
1 =>
file_path => /7u3pxc0K1wx32IleAkLv78MKgrw.jpg
width => 1920
height => 1080
iso_639_1 =>
aspect_ratio => 1.78
vote_average => 5.4509803921569
vote_count => 22
2 =>
etc etc....
我尝试过这样做,但无法获得任何数据:
foreach($theMovieData['images']['backdrops'][0]['vote_count'] as $key => $item) {
echo $item;
}
我做错了什么?如何在找到最高票数后获得 file_path ?
感谢您的帮助!
答案 0 :(得分:1)
foreach循环用于循环遍历数组,您尝试在发布的代码示例中实现的是单个变量中的循环。您应该将代码更改为:
$max = 0;
$pathMax = null;
foreach ($theMovieData['images']['backdrops'] as $data){
$voteCount = $data['vote_count'];
$path = $data['file_path'];
if ((int)$voteCount > $max){
$max = (int)$voteCount;
$pathMax = $path;
}
}
答案 1 :(得分:0)
您需要迭代backdrops
,而不是votecount
。还可以使用临时变量来存储和比较之前的值。
$temp = -1;
foreach($theMovieData['images']['backdrops'] as $key => $item) {
if ($item["vote_count"] > $temp)
$temp = $item["vote_count"];
}