dqouryz3595 2013-03-15 20:27
浏览 10
已采纳

PHP循环遍历数组并找到最高值

Hey all i am trying to see what the highest vote count is within this array and then get the file_path from that.

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?

Thanks for the help!

  • 写回答

2条回答 默认 最新

  • dp411805872 2013-03-15 20:33
    关注

    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. 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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”