Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否可以在foreach in codeigniter中使用foreach_Php_Json_Codeigniter - Fatal编程技术网

Php 是否可以在foreach in codeigniter中使用foreach

Php 是否可以在foreach in codeigniter中使用foreach,php,json,codeigniter,Php,Json,Codeigniter,这是我的函数-我得到一个错误 在变量$data['stars']中,我得到的最大星星数无效 我想从数据库中得到正确的星号,因为实际上星号表是不同的,所以这就是为什么 是否有可能从这个foreach中的另一个表中获取星星以获得准确的值 public function dbmovies() { $this->db->order_by("videos_id", "desc"); $this->db->where(array('publication' =>

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

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

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

是否有可能从这个foreach中的另一个表中获取星星以获得准确的值

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);
}
我得到的输出是这样的

{
    "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 didn\u2019t 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"
        },

{
“数据”:[
{
“videoId”:“47”,
“imdbid”:“tt2935510”,
“标题”:“广告阿斯特拉”,
“说明”:"在不久的将来,希望和困难都驱使人类去仰望星空和更远的地方。虽然一种神秘的现象威胁着地球上的生命,但宇航员罗伊·麦克布莱德(Roy McBride)承担着穿越浩瀚太空及其众多危险的使命,以揭示几十年前勇敢面对emp的一次失败探险的真相在寻找未知的过程中保持沉默。”,
“持续时间”:“123分钟”,
“视图”:“2”,
“明星”:“布拉德·皮特”
},
{
“videoId”:“45”,
“imdbid”:“tt8243160”,
“标题”:“黑客”,
“描述”:“13岁的本杰明发现他的母亲并没有像他相信的那样死于事故。这条线索指向丹麦特勤局的高级官员。他被告知“不要相信任何人!”,
“持续时间”:“96分钟”,
“意见”:“93”,
“明星”:“布拉德·皮特,鲁姆·克鲁斯”
},
{
“videoId”:“44”,
“imdbid”:“tt7131622”,
“标题”:“从前……在好莱坞”,
“描述”:“在1969年洛杉矶好莱坞黄金时代的最后几年里,一位退色的电视演员和他的特技演员努力在电影业获得名望和成功。”,
“持续时间”:“161分钟”,
“意见”:“71”,
“明星”:“布拉德·皮特、鲁姆·克鲁斯、莱昂纳多·迪卡普里奥”
},

有人能帮我吗?

在foreach$stars=array()之前添加空数组;

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

您必须将
$starone
变量移动到
foreach
循环的外部,以便它不会在循环内部被覆盖:

foreach($queryStars->result()作为$star){
$stars[]=$star->star\u名称;
}
$starone=内爆(“,”,$stars);

您可以使用join Query进行此操作!无论如何,我更新了您的问题,试试看!!虽然有效,但我只得到了一颗星,而且每个视频几乎至少有两颗星。实际上,它包含类似于“156720620972098”的星号这和我们从db检查id并获取星号请分享更多详细信息-您问题的标题不反映您提供的代码