一维数组分组成二维数组

目标

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 app\api\model;

    use app\common\model\ModelBase;
    use think\Db;

    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');
//        }

    }
posted @ 2020-10-31 16:31  HaimaBlog  阅读(133)  评论(0编辑  收藏  举报