0

嘿,我想看看这个数组中的最高票数是多少,然后从中获取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

谢谢您的帮助!

4

2 回答 2

2

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;
    }
}
于 2013-03-15T20:33:59.597 回答
0

您需要迭代backdrops,而不是votecount. 还使用临时变量来存储和比较先前的值。

$temp = -1;
foreach($theMovieData['images']['backdrops'] as $key => $item) {

if ($item["vote_count"] > $temp)
$temp = $item["vote_count"];
}
于 2013-03-15T20:32:24.683 回答