zoukankan      html  css  js  c++  java
  • 一维数组分组成二维数组

    目标

    goods_spec里相同attr_id的数组分成一组,然后再放回goods_spec

    原始数据

    {
        "code": 200,
        "msg": "success",
        "data": {
            "id": 1,
            "goods_name": "情感挽回方案 ",
            "goods_sn": "g0001",
            "cate_id": 1,
            "meals_number": 1,
            "shop_price": 1000,
            "pur_price": 1000,
            "vip_price": 1000,
            "keyword": "情感挽回方案 ",
            "goods_desc": "情感挽回方案 ",
            "goods_details": "情感挽回方案 ",
            "image_logo": "/static/images/shop01.jpg",
            "sort": 50,
            "seller_note": "",
            "is_delete": 0,
            "status": 1,
            "created_at": "2019-11-21 10:34:26",
            "updated_at": "2019-11-21 10:34:26",
            "goods_spec": [
                {
                    "id": 1,
                    "goods_id": 1,
                    "name": "规格",
                    "value": "金百合1",
                    "code": null,
                    "attr_price": "0.00",
                    "attr_id": 1
                },
                {
                    "id": 2,
                    "goods_id": 1,
                    "name": "规格",
                    "value": "金百合2",
                    "code": null,
                    "attr_price": "10.00",
                    "attr_id": 1
                },
                {
                    "id": 3,
                    "goods_id": 1,
                    "name": "颜色",
                    "value": "红色",
                    "code": null,
                    "attr_price": "0.00",
                    "attr_id": 2
                },
                {
                    "id": 4,
                    "goods_id": 1,
                    "name": "颜色",
                    "value": "红色",
                    "code": null,
                    "attr_price": "5.00",
                    "attr_id": 2
                }
            ]
        }
    }
    

    目标结果:

    {
        "code": 200,
        "msg": "success",
        "data": {
            "id": 1,
            "goods_name": "情感挽回方案 ",
            "goods_sn": "g0001",
            "cate_id": 1,
            "meals_number": 1,
            "shop_price": 1000,
            "pur_price": 1000,
            "vip_price": 1000,
            "keyword": "情感挽回方案 ",
            "goods_desc": "情感挽回方案 ",
            "goods_details": "情感挽回方案 ",
            "image_logo": "/static/images/shop01.jpg",
            "sort": 50,
            "seller_note": "",
            "is_delete": 0,
            "status": 1,
            "created_at": "2019-11-21 10:34:26",
            "updated_at": "2019-11-21 10:34:26",
            "goods_spec": {
                "规格": [
                    {
                        "id": 1,
                        "goods_id": 1,
                        "name": "规格",
                        "value": "金百合1",
                        "code": null,
                        "attr_price": "0.00",
                        "attr_id": 1
                    },
                    {
                        "id": 2,
                        "goods_id": 1,
                        "name": "规格",
                        "value": "金百合2",
                        "code": null,
                        "attr_price": "10.00",
                        "attr_id": 1
                    }
                ],
                "颜色": [
                    {
                        "id": 3,
                        "goods_id": 1,
                        "name": "颜色",
                        "value": "红色",
                        "code": null,
                        "attr_price": "0.00",
                        "attr_id": 2
                    },
                    {
                        "id": 4,
                        "goods_id": 1,
                        "name": "颜色",
                        "value": "红色",
                        "code": null,
                        "attr_price": "5.00",
                        "attr_id": 2
                    }
                ]
            }
        }
    }
    
    

    thinkphp5 model 代码

    <?php
     
        namespace appapimodel;
    
        use appcommonmodelModelBase;
        use thinkDb;
    
        class Goods extends ModelBase
        {
            // 数据库表前缀
    //        protected $connection = ['prefix'=> ''];
    //        protected $update = ["update_time"];
    //        protected $table = 'user';
            protected $pk    = "id";
    
            // 追加属性
            protected $append = [
    //        "goods_spec", "selling_price", "checked", "goods_info"
                "goods_spec"
            ];
    
            public function getGoodsSpecAttr($value, $data)
            {
                $goods_id = $data['id'];
                $goodsSpecInfo =  db("goods_spec")
                    ->where("goods_id", $goods_id)
                    ->select();
                return $this->getArrayGroup($goodsSpecInfo) ;
            }
    
            //一组数组分组成二维数组
            //$arrayAll 一维数组   
            function getArrayGroup($arrayAll){
                $goodsAttrIds = [];
                foreach ($arrayAll as $k =>$v){
                    array_push($goodsAttrIds,$v["attr_id"]);
                }
                $goodsAttrIds = array_unique($goodsAttrIds);
                $goodsSpec = [];
                $goodsSpecTmp = [];
                $nameTmp = '';
                foreach ($goodsAttrIds as $k =>$v){
                    foreach ($arrayAll as $ks =>$gSpec){
                        if ($v == $gSpec["attr_id"]){
                            $nameTmp = $gSpec["name"];
                            array_push($goodsSpecTmp,$gSpec);
                        }
                    }
                    $goodsSpec[$nameTmp] = $goodsSpecTmp;
                    $goodsSpecTmp = [];
                    $nameTmp = '';
                }
                return $goodsSpec;
            }
    
            /**
             * @return mixed
             * @author: haima
             * @name: setUpdateTimeAttr
             * @describe:
             */
            protected function setUpdateTimeAttr()
            {
                return time();
            }
    
    
             //获取指定id的商品
            public function GetGoodsDetailById($id){
                return $this->find($id)->toArray();
            }
            
            //一对多
    //        public function GetGoodsDetailById($id){
    //            $goodsInfo =   $this
    ////                ->with(['getGoodsSpec'])
    //                ->find($id)->toArray();
    //
    //            return $goodsInfo;
    //        }
    
            //一对一关联查询lb_user_ext
    //        public function getGoodsSpec()
    //        {
    ////            return $this->belongsTo('goods_spec','id','goods_id');
    //            return $this->hasMany('goods_spec','goods_id');
    //        }
    
        }
    
  • 相关阅读:
    织梦分页条添加省略号(支持动态静态)
    织梦点击数或者其他数值过【千】过【万】过【亿】的写法
    织梦文章页每个TAG标签单独输出相关文章
    织梦验证码不显示解决方法总结
    织梦搜索结果根据搜索不同栏目显示不同搜索结果模板
    织梦正则提取中英混合字符串中第一个中文汉字
    织梦让内容摘要多行文本支持换行
    织梦dede:tag标签输入添加自增autoindex
    织梦去除底部版权power by dedecms
    织梦自定义表单添加访客提交时间和访客IP+限制每天每个IP提交表单次数
  • 原文地址:https://www.cnblogs.com/haima/p/13906658.html
Copyright © 2011-2022 走看看