[英]PHP loop through array and find highest value
Hey all i am trying to see what the highest vote count is within this array and then get the file_path from that. 嘿所有我想看看这个数组中最高的投票数是多少,然后从那里得到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....
I tried doing this but was unable to get any data: 我尝试这样做但无法获得任何数据:
foreach($theMovieData['images']['backdrops'][0]['vote_count'] as $key => $item) {
echo $item;
}
What would i be doing incorrect? 我做错了什么? And how would i get the file_path after finding the highest vote ?
如何在找到最高票数后获得file_path ?
Thanks for the help! 谢谢您的帮助!
The foreach loop is meant to loop through an array, what you are trying to achieve in the code sample posted is a loop in a single variable. foreach循环用于遍历数组,您尝试在发布的代码示例中实现的是单个变量中的循环。 You should change the code to something like:
您应该将代码更改为:
$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;
}
}
You need to iterate backdrops
, not votecount
. 你需要迭代
backdrops
,而不是votecount
。 Also use a temporary variable to store and compare the previous value. 还可以使用临时变量来存储和比较之前的值。
$temp = -1;
foreach($theMovieData['images']['backdrops'] as $key => $item) {
if ($item["vote_count"] > $temp)
$temp = $item["vote_count"];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.