2011-05-16 42 views
0

對不起,我對這個話題一無所知,但當我遇到我的PHP出現問題時,我真的不知道該去哪裏看看這個網站。壞陣列?壞的foreach?

我想在這裏做的是使用預先指定的ID從數據庫調用特定的電影。但是我所得到的是在下面的第二和第三個foreach中爲'foreach()'提供的'無效參數'。

這裏是我的頭代碼:

//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; 
} 

而在躋身體:

// 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 
    } 

的小測試中,我所做的就是檢查什麼$list_chosen$list_chosen_film$films_result$film實際上包含通過將它們打印在頁面的底部。

$list_chosen節目 - Array$list_chosen_film節目 - 42194$films_result顯示整個JSON字符串,$film節目 - Array

+0

結果是什麼:var_dump($ film)? – haknick 2011-05-16 01:04:23

+0

這裏是var_dump($ film)結果的開始: 'array(1){[0] => object(stdClass)#249(33){[「popular」] => int(3)[「translation 「] =>布爾(真)......' 我會發布整個,但它太大了 – Rowan 2011-05-16 16:25:35

+0

爲什麼不是這篇文章得到關注,那麼,如果發佈它的確切副本呢?哪一個關閉? – Rowan 2011-05-16 22:17:22

回答

1

發生這種情況是因爲 - 由於PHP顯示的錯誤通知了您 - 您提供了錯誤的參數給foreach循環,可能是null或其他值。確保你提供了數組來foreach。

此外,每次使用foreach時間,那樣做:

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 
} 

這將確保你不會看到錯誤,只是因爲沒有在名單上。

編輯:

documentation page,你可以找到一些小費如何避免這樣的錯誤,如果你想通過遍歷集合爲空。只需將該集合投射到array類型:

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

您能否提供$ film的轉儲?該錯誤告訴你,你正在指向一個無法迭代的對象(最有可能爲空)。

+0

var_dump($ film)的結果如下: 'array(1){[0] => object(stdClass)#249(33){[「popular」] => int(3)[「translation 「] =>布爾(真)......' 我會發布整個很多,但它太大了 – Rowan 2011-05-16 16:25:47

+0

轉儲$電影 - http://freetexthost.com/4cfneqdqty – Rowan 2011-05-17 00:15:46

2

嘗試增加:

print_r($film->backdrop); 

第二foreach()循環之前。錯誤消息之前它不會是一個數組,或者它將包含零個元素(不允許)。如果您還添加:

echo $films_result; 

您將能夠調試它並完全理解錯誤。如果不是,請在問題中發佈整個輸出。

+0

我做了'print_r ($電影 - >背景);'和它什麼也不印。我爲'$ list_chosen','$ list_chosen_film','$ films_result','$ film'完成了相同的操作,並且它們看起來都很完美......在這裏我保存了整個'$ film'輸出 - > http: //freetexthost.com/4cfneqdqty。我看起來很好嗎?背景在那裏和每個人 – Rowan 2011-05-17 00:14:49