是否可以在代码点火器中的foreach中使用foreach



这是我的函数-我得到了一个错误。

在变量$data['stars']中,我得到了无效的最大恒星数

我想从数据库中得到正确的星星,因为实际上星星表是不同的,所以这就是为什么。

有可能从这个前臂的另一张表中获得星星来获得准确的值吗?

public function dbmovies()
{
$this->db->order_by("videos_id", "desc");
$this->db->where(array('publication' => 1, 'is_tvseries' => 0));
$query = $this->db->get('videos');
//get stars
foreach ($query->result() as $key => $value) {
$this->db->where_in('star_id', $value->stars);
$queryStars = $this->db->get('star');
$stars=array();
foreach ($queryStars->result() as $star) {
$stars[] = $star->star_name;
$starone = implode(", ", $stars);
}
$data['videoId'] = $value->videos_id;
$data['imdbid'] = $value->imdbid;
$data['title'] = $value->title;
$data['description'] = $value->description;
$data['duration'] = $value->runtime;
$data['views'] = $value->total_view;
$data['stars'] = $starone;
$alldata[] = $data;
}
$get['data'] = $alldata;
echo json_encode($get, JSON_PRETTY_PRINT);
}

我得到的tge输出像这个

{
"data": [
{
"videoId": "47",
"imdbid": "tt2935510",
"title": "Ad Astra",
"description": "<p>The near future, a time when both hope and hardships drive humanity to look to the stars and beyond. While a mysterious phenomenon menaces to destroy life on planet Earth, astronaut Roy McBride undertakes a mission across the immensity of space and its many perils to uncover the truth about a lost expedition that decades before boldly faced emptiness and silence in search of the unknown.</p>",
"duration": "123 Min",
"views": "2",
"stars": "Brad Pitt"
},
{
"videoId": "45",
"imdbid": "tt8243160",
"title": "Hacker",
"description": "<p>13-year-old Benjamin discovers that his mother didnu2019t die in an accident as he was led to believe. The trail leads to high-ranking officials in the Danish Secret Service. "Trust no one!", he is told.</p>",
"duration": "96 Min",
"views": "93",
"stars": "Brad Pitt, Rumle Krs"
},
{
"videoId": "44",
"imdbid": "tt7131622",
"title": "Once Upon a Time... in Hollywood",
"description": "<p>A faded television actor and his stunt double strive to achieve fame and success in the film industry during the final years of Hollywood's Golden Age in 1969 Los Angeles.</p>",
"duration": "161 Min",
"views": "71",
"stars": "Brad Pitt, Rumle Krs, Leonardo DiCaprio"
},

有人能帮我吗?

您必须将$starone变量移动到foreach循环的外部,这样它就不会在循环中被覆盖:

foreach ($queryStars->result() as $star) {
$stars[] = $star->star_name;
}
$starone = implode(", ", $stars);

foreach前添加空数组$stars=array((

$stars=array();
foreach ($queryStars->result() as $star) {
$stars[] = $star->star_name;
$starone = implode(", ", $stars);
}

最新更新