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');
    //        }
    
        }
    
  • 相关阅读:
    参考vue.js实现双向绑定的方法理解双向绑定原理(:Object.defineProperty和发布-订阅模式)
    不错的站点 博文
    使用C#动态生成Word文档/Excel文档的程序测试通过后,部署到IIS服务器上,不能正常使用的问题解决方案
    详解HTML<head> 头标签元素的意义以及使用场景
    css奇特用法之 IMG添加背景图片配合显示--效果惊艳
    .net面试题-15k+左右
    微信小程序IOS真机调试发生了SSL 错误,无法建立与该服务器的安全连接
    微信小程序自定义组件-下拉框
    微信小程序语音(A)发给别人(B),也能播放,是需要先把语音上传到自己的服务器上才可以
    微信小程序循环中点击一个元素,其他的元素不发生变化,类似点击一个循环中的语音,其他的不发生点击事件
  • 原文地址:https://www.cnblogs.com/haima/p/13906658.html
Copyright © 2011-2022 走看看