dongnao1908 2011-05-16 00:57
浏览 51

坏阵列? 不好的foreach?

Sorry for the huge ignorance on the topic, but I really have no idea where to look other than this website when I come into trouble with my PHP.

What I'm trying to do here is use pre-designated IDs to call particular movies from a database. But all I get is an 'Invalid argument supplied for foreach()' message on the second and third foreach's below.

Here's my code in the head:

//Custom lists of movies to bring in
//New Releases list
 $films_new_releases = array(40805, 46705, 41630, 44564, 39451, 20352, 43933, 49009, 49797, 42194);
 //Most Popular list
 $films_most_popular = array(27205, 16290, 10138, 41733, 37799, 18785, 19995, 17654, 10140, 12162);

//Get information from address bar
$list = $_GET['l'];
if ($list == 'new releases') {
    $list_chosen = $films_new_releases;
}
elseif ($list == 'most popular') {
    $list_chosen = $films_most_popular;
}
else {
    $list_chosen = $films_new_releases;
}

And in amongst the body:

  // Loop through each film returned
  foreach ($list_chosen as $list_chosen_film) {

    $films_result = $tmdb->getMovie($list_chosen_film);
    $film = json_decode($films_result);

    // Set default poster image to use if film doesn't have one
    $backdrop_url = 'images/placeholder-film.gif';

    // Loop through each poster for current film
    foreach($film->backdrops as $backdrop) {
      if ($backdrop->image->size == 'poster') {
        $backdrop_url = $backdrop->image->url;
      }
    }

    echo '<div class="view-films-film">
        <a href="film.php?id=' . $film->id . '"><img src="' . $backdrop_url . '" alt="' . $film->name . '" /></a>
            <div class="view-films-film-snippet">
                <h2><a href="film.php?id=' . $film->id . '">' . $film->name . '</a></h2>';
    if ($film->certification != null) {
           echo '<img src="images/bbfc-' . strtolower($film->certification) . '.png" alt="" />';
    }
    echo '      <h3>Starring</h3>
                <p>';
    $num_actors = 0;
    foreach ($film->cast as $cast) {
    if ($cast->job == 'Actor') {
      echo '<a href="person.php?id=' . $cast->id . '">' . $cast->name . '</a> ';
      $num_actors++;
      if ($num_actors == 5)
        break;
    }
    echo '      </p>
                <h3>Director</h3>
                <p>';
    foreach ($film->cast as $cast) {
        if ($cast->job == 'Director') {
            echo '<a href="person.php?id=' . $cast->id . '">' . $cast->name . '</a> ';
        }
    }
    echo '      </p>
            </div>
        </div>';
  }
  // End films
  }

The little testing I've done is checking what $list_chosen, $list_chosen_film, $films_result and $film actually contain by printing them at the bottom of the page.

$list_chosen shows - Array, $list_chosen_film shows - 42194, $films_result shows the entire JSON string, $film shows - Array.

  • 写回答

3条回答 默认 最新

  • dongzhong2018 2011-05-16 01:06
    关注

    This happens, because - as error displayed by PHP informed you - you have provided wrong parameter to foreach loop, probably null or some other value. Make sure you are providing array to foreach.

    Also, every time you use foreach, do it like that:

    if (count($some_list) > 0) {
        foreach ($some_list as $list_item) {
            // code for each item on the list
        }
    } else {
        // code when there is nothing on the list
    }
    

    This will ensure you will not see errors just because there is nothing on the list.

    EDIT:

    On the documentation page you can find some tip how to avoid such errors if the collection you are trying to iterate through is empty. Just cast the collection to array type:

    foreach ((array) $some_list as $list_item) {
        // code for each item on the list
    }
    
    评论

报告相同问题?