0

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!

1
  • 1
    do the foreach on $theMovieData['images']['backdrops']. you're trying to loop on an integer in that array, not on an array element. Commented Mar 15, 2013 at 20:29

2 Answers 2

2

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to iterate backdrops, not 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"];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.