Php 糟糕的阵列?糟糕的foreach?
很抱歉,我对这个话题一无所知,但当我的PHP遇到麻烦时,我真的不知道除了这个网站以外,我还能去哪里看看 我在这里尝试的是使用预先指定的ID从数据库中调用特定的电影。但我得到的只是下面第二个和第三个foreach上的“为foreach()提供的参数无效”消息 下面是我头脑中的代码:Php 糟糕的阵列?糟糕的foreach?,php,arrays,json,Php,Arrays,Json,很抱歉,我对这个话题一无所知,但当我的PHP遇到麻烦时,我真的不知道除了这个网站以外,我还能去哪里看看 我在这里尝试的是使用预先指定的ID从数据库中调用特定的电影。但我得到的只是下面第二个和第三个foreach上的“为foreach()提供的参数无效”消息 下面是我头脑中的代码: //Custom lists of movies to bring in //New Releases list $films_new_releases = array(40805, 46705, 41630, 44
//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
}
//循环检查返回的每个胶片
foreach($list\u选择为$list\u选择的电影){
$films\u result=$tmdb->getMovie($list\u selected\u film);
$film=json\u decode($films\u result);
//将默认海报图像设置为在胶片没有海报图像时使用
$background_url='images/placeholder film.gif';
//循环浏览当前电影的每张海报
foreach($film->背景作为$background){
如果($background->image->size=='poster'){
$background\u url=$background->image->url;
}
}
回声'
';
如果($film->certification!=null){
echo“认证”。.png“alt=”“/>”;
}
《回声》主演
",;
$num_actors=0;
foreach($film->cast as$cast){
如果($cast->job=='Actor'){
回声';
$num_actors++;
如果($num_actors==5)
打破
}
回声'
经理
",;
foreach($film->cast as$cast){
如果($cast->job=='Director'){
回声';
}
}
回声'
';
}
//末膜
}
我所做的小测试是通过在页面底部打印它们来检查$list\u selected
、$list\u selected\u film
、$films\u result
和$film
实际包含的内容
$list\u selected
显示-Array
,$list\u selected\u film
显示-42194
,$films\u result
显示整个JSON字符串,$film
显示-Array
发生这种情况,因为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
}
这将确保您不会因为列表中没有任何内容而看到错误
编辑:
在上,您可以找到一些技巧,如何在您尝试迭代的集合为空时避免此类错误。只需将集合强制转换为数组
类型:
foreach ((array) $some_list as $list_item) {
// code for each item on the list
}
发生这种情况的原因是,正如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
}
这将确保您不会因为列表中没有任何内容而看到错误
编辑:
在上,您可以找到一些技巧,如何在您尝试迭代的集合为空时避免此类错误。只需将集合强制转换为数组
类型:
foreach ((array) $some_list as $list_item) {
// code for each item on the list
}
是否可以提供$film转储?错误是指无法迭代的对象(很可能为空)。是否可以提供$film转储?错误是指无法迭代的对象(很可能为空)。尝试添加:
print_r($film->backdrop);
在第二个foreach()循环之前。在错误消息之前,它将不是数组或包含零元素(不允许)。如果您还添加:
echo $films_result;
您将能够调试它并完全理解错误所在。如果没有,请在问题中发布整个输出。尝试添加:
print_r($film->backdrop);
在第二个foreach()循环之前。在错误消息之前,它将不是数组或包含零元素(不允许)。如果您还添加:
echo $films_result;
您将能够调试它并完全理解错误所在。如果没有,请在问题中发布整个输出。var_dump($film)的结果是什么?下面是var_dump($film)结果的开始:
array(1){[0]=>object(stdClass){[“popularity”]=>int(3)[“translated”]=>bool(true)…
我会发布全部内容,但它太大了。如果发布一个完全相同的内容,为什么这篇文章没有引起注意?然后关闭了?结果是什么:var_dump($film)?这里是var_dump($film)结果的开始:数组(1){[0]=>object(stdClass){249(33){[“popularity”=>int(3)[“translated”]=>bool(真的)…
我会发布全部内容,但它太大了。如果发布一个完全相同的内容,为什么这篇文章没有引起注意呢?然后关闭?下面是var_dump($film)结果的开始:array(1){[0]=>object(stdClass){249(33){[“popularity”]=>int(3)[“translated”]=>bool(真的)…
我会发布全部内容,但它太大了var_dump($film)的结果是这样的:array(1){[0]=>object(stdClass){[“popularity”]=>int(3)[“translated”]=>bool(true)…
我会发布全部内容,但它太大了,print\r($film->背景)
它什么也没打印。我对$list\u selected
,$list\u selected\u film
,$films\u result
,$film
做了同样的处理。我保存了整个$film
输出->。它看起来很好??背景在那里,每个人都做了打印($film->
,它什么也没打印。我对$list\u selected
,$list\u selected\u film
,$films\u result
,$film
做了同样的处理。我保存了整个$film
输出。它看起来很好??背景就在那里,每个人都在那里