循环运用(多级嵌套结构)

    //将系列的标签同步到单个粒度课程
    //逻辑 将系列的私有与共有标签都写到activity课程表中的 private_tags
    public function setPackThemeWord($pri_tags,$com_tags,$id)
    {
        //参数位置
        $private_tags = $pri_tags;
        $tags = $com_tags;
        $pack_id = $id;

        //检测与拼接
        $pri = "";
        if(isset($private_tags) && !empty($private_tags)){
            $pri .= $private_tags;
        }
        if(isset($tags) && !empty($tags) && !empty($private_tags)){
            $pri .= ",". $tags;
        }
        if(isset($tags) && !empty($tags) && empty($private_tags)){
            $pri .= $tags;
        }
        if(empty($pri)) return false;
        //字符处理
        $arr = explode(",",$pri);
        $arr_unique = array_unique($arr);
        $str = "";
        foreach ($arr_unique as $v){
            $str .= $v.",";
        }
        $str = rtrim($str, ',');

        //数据同步
        $pack = Pack::where("pack_id",$pack_id)->get()->toArray();
        if(empty($pack) || !isset($pack[0]["theme_setting"]) || empty($pack[0]["theme_setting"])) return false;
        $theme = json_decode($pack[0]["theme_setting"],true);
//        echo "<pre>";
//        print_r($theme);

        $space = array();
        if(isset($theme["age4"])){
            $space[] = $theme["age4"];
        }
        if(isset($theme["age5"])){
            $space[] = $theme["age5"];
        }
        if(isset($theme["age6"])){
            $space[] = $theme["age6"];
        }

        foreach($space as $num){
            //目录层级2
            foreach($num["theme"] as $v2){
                foreach($v2["activity"] as $v3){
                    //单个产品课程
                    $curr = DB::connection('zino')->table("activity")->where("aid",$v3["aid"])->get()->toArray();//未查询到未空数组
                    if(empty($curr)) continue;
                    if(!empty($curr[0]["private_tags"])){
                        //不为空:提取 解析 合并 去重 拼接 修改
                        $arr_one = explode(",",$curr[0]["private_tags"]);
                        $array_mer = array_merge($arr_one,$arr_unique);
                        $pritags = array_unique($array_mer);
                        $str2 = "";
                        foreach ($pritags as $value){
                            $str2 .= $value.",";
                        }
                        $str2 = rtrim($str2, ',');
                        $res = DB::connection('zino')->table("activity")->where("aid",$v3["aid"])->update(['private_tags'=>$str2]);//如果执行成功返回1 失败0
                    }else{
                        //为空的情况
                        DB::connection('zino')->table("activity")->where("aid",$v3["aid"])->update(['private_tags'=>$str]);
                    }
                }
            }
        }
        return true;
    }
posted @ 2020-03-19 20:45  锐庆  阅读(368)  评论(0)    收藏  举报