嵌套的 foreach 语句无效。帮助?
请原谅极端初学者的编码风格。我第一次使用 API 中的 PHP 和 JSON 字符串。
我在这里试图做的是打印出用户正在搜索的多个电影结果,如果你仔细查看它显然是行不通的。这是一个搜索结果页面 - 每个项目旁边都有电影海报和一些关键详细信息。
我想要每个结果旁边的一件事是电影中列为“主演”的前 5 位演员的列表,然后是电影中列为“导演”的所有导演。
毫无疑问,问题在于我嵌套了 foreach 语句。但我不知道还有什么其他方法可以做到这一点。
请拜托,最简单的答案对我来说是最好的。我不介意这是否是不好的做法,只要能最快解决问题就完美了!
代码如下:
// Check if there were any results
if ($films_result == '["Nothing found."]' || $films_result == null) {
}
else {
// Loop through each film returned
foreach ($films as $film) {
// 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 == 'thumb') {
$backdrop_url = $backdrop->image->url;
}
}
echo '<div class="view-films-film">
<a href="film.php?id=' . $film->id . '"><img src="' . $backdrop_url . '" alt="' . strtolower($film->name) . '" /></a>
<div class="view-films-film-snippet">
<h2><a href="film.php?id=' . $film->id . '">' . strtolower($film->name) . '</a></h2>
<img src="images/bbfc-' . strtolower($film->certification) . '.png" alt="" />
<h3>Starring</h3>
<p>';
$num_actors = 0;
foreach ($films[0]->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 ($films[0]->cast as $cast) {
if ($cast->job == 'Director') {
echo '<a href="person.php?id=' . $cast->id . '">' . $cast->name . '</a> ';
}
}
echo ' </p>
</div>
</div>';
// End films
}
}
结果示例如下:
第 120 行为 foreach ($films [0]->cast as $cast) {
并且第 131 行是 foreach ($films[0]->cast as $cast) {
Please forgive the extreme-beginner style of coding. I'm working with PHP and JSON strings from an API for the first time.
What I'm trying to do here, which obviously doesn't work if you look through it, is print out multiple movie results that the user is searching for. This is a search results page - with a movie poster and some key details next to each item.
One of the things I want next to each result is a list of the first 5 actors listed in the movie, as "Starring" and then any directors listed in the movie as "Director".
The problem is no doubt the fact that I've nested foreach statements. But I don't know any other way of doing this.
Please please the simplest answer would be the best for me. I don't mind if it's bad practice, just whatever solves it quickest would be perfect!
Here's the code:
// Check if there were any results
if ($films_result == '["Nothing found."]' || $films_result == null) {
}
else {
// Loop through each film returned
foreach ($films as $film) {
// 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 == 'thumb') {
$backdrop_url = $backdrop->image->url;
}
}
echo '<div class="view-films-film">
<a href="film.php?id=' . $film->id . '"><img src="' . $backdrop_url . '" alt="' . strtolower($film->name) . '" /></a>
<div class="view-films-film-snippet">
<h2><a href="film.php?id=' . $film->id . '">' . strtolower($film->name) . '</a></h2>
<img src="images/bbfc-' . strtolower($film->certification) . '.png" alt="" />
<h3>Starring</h3>
<p>';
$num_actors = 0;
foreach ($films[0]->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 ($films[0]->cast as $cast) {
if ($cast->job == 'Director') {
echo '<a href="person.php?id=' . $cast->id . '">' . $cast->name . '</a> ';
}
}
echo ' </p>
</div>
</div>';
// End films
}
}
An example of the result would be this:
with line 120 being foreach ($films[0]->cast as $cast) {
and line 131 being foreach ($films[0]->cast as $cast) {
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么在 foreach 中使用 $films[0] ($films 作为 $film)?您应该使用 $film->cast 对吗?并且您应该在循环开始时使用 var_dump 转储 $film 并检查它 - 确保 $cast 是一个数组并且已填充。如果不是,请从那里返回。
Why are you using $films[0] when you're in a foreach ($films as $film)? You should be using $film->cast right? And you should dump $film at the start of your loop with var_dump and inspect it - make sure $cast is an array and is populated. If it's not, work back from there.