账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    php 双重foreach的坑
    26
    0
    $cardInfo 的JSON 格式如下
    {
        "code": 1,
        "msg": "success",
        "time": 1524036614,
        "data": {
            "list": [
                {
                    "id": 1000,
                    "user_id": 1,
                    "live_hoster_id": 1,
                    "goods_id": 1,
                    "goods_unit": "个",
                    "goods_num": 1,
                    "goods_price_id": 2,
                    "create_time": 0,
                    "update_time": 0,
                    "is_delete": 0,
                    "delete_time": null,
                    "business_id": 1,
                    "shop": "测试商家1",
                    "goodsInfo": {
                        "id": 1,
                        "name": "测试商品1",
                        "sku": "00000000",
                        "category_id": 0,
                        "business_id": 1,
                        "sale_status": 1,
                        "sort": 0,
                        "price": "0.00",
                        "stock_num": 0,
                        "saled_num": 0,
                        "thumbnail": " ",
                        "create_time": 0,
                        "update_time": 0,
                        "is_delete": 0,
                        "delete_time": null,
                        "priceInfo": {
                            "id": 2,
                            "price": "80.00",
                            "sale_price": "60.00",
                            "host_per": "10.00",
                            "update_time": 0,
                            "is_delete": 0,
                            "delete_time": null
                        }
                    }
                },
                {
                    "id": 1001,
                    "user_id": 1,
                    "live_hoster_id": 1,
                    "goods_id": 2,
                    "goods_unit": "个",
                    "goods_num": 1,
                    "goods_price_id": 1,
                    "create_time": 0,
                    "update_time": 0,
                    "is_delete": 0,
                    "delete_time": null,
                    "business_id": 2,
                    "shop": "测试商家2",
                    "goodsInfo": {
                        "id": 2,
                        "name": "测试商品2",
                        "sku": "00000000",
                        "category_id": 0,
                        "business_id": 2,
                        "sale_status": 1,
                        "sort": 0,
                        "price": "0.00",
                        "stock_num": 0,
                        "saled_num": 0,
                        "thumbnail": " ",
                        "create_time": 0,
                        "update_time": 0,
                        "is_delete": 0,
                        "delete_time": null,
                        "priceInfo": {
                            "id": 1,
                            "price": "100.00",
                            "sale_price": "99.00",
                            "host_per": "20.00",
                            "update_time": 0,
                            "is_delete": 0,
                            "delete_time": null
                        }
                    }
                },
                {
                    "id": 1002,
                    "user_id": 1,
                    "live_hoster_id": 1,
                    "goods_id": 3,
                    "goods_unit": "个",
                    "goods_num": 1,
                    "goods_price_id": 3,
                    "create_time": 0,
                    "update_time": 0,
                    "is_delete": 0,
                    "delete_time": null,
                    "business_id": 2,
                    "shop": "测试商家2",
                    "goodsInfo": {
                        "id": 3,
                        "name": "测试商品3",
                        "sku": "00000000",
                        "category_id": 0,
                        "business_id": 2,
                        "sale_status": 1,
                        "sort": 0,
                        "price": "0.00",
                        "stock_num": 0,
                        "saled_num": 0,
                        "thumbnail": " ",
                        "create_time": 0,
                        "update_time": 0,
                        "is_delete": 0,
                        "delete_time": null,
                        "priceInfo": {
                            "id": 3,
                            "price": "200.00",
                            "sale_price": "180.00",
                            "host_per": "20.00",
                            "update_time": 0,
                            "is_delete": 0,
                            "delete_time": null
                        }
                    }
                }
            ]
        }
    }

    想变换数组结构为

    {
        "code": 1,
        "msg": "success",
        "time": 1524036789,
        "data": {
            "list": [
                {
                    "shop_id": 1,
                    "shop_name": "测试商家1",
                    "goodsInfo":[
                        {goods1},{goods2}
                    ]
                },
                {
                    "shop_id": 2,
                    "shop_name": "测试商家2",
                    "goodsInfo":[
                        {goods3},{goods4}
                    ]
                }
            ]
        }
    }

    于是有了如下代码

            $cartInfo = collection($cartItems)->toArray();
            $shops =array_column($cartInfo,'shopInfo','business_id');
            $shop_list = [];
            $shop_lists = [];
            foreach ($shops as $key => $shop){
                foreach ($cartInfo as $k=>$item){
                    if($item['business_id'] === $key  && $item['shopInfo']['id'] === $key){
                       $shop_list['shop_id'] = $item['business_id'];
                        $shop_list['shop_name'] = $item['shopInfo']['name'];
                        $shop_list['goodsInfo'][] = $item['goodsInfo'];
                    }
                }
                $shop_lists[] = $shop_list;
            }
            return $shop_lists;

    但是出来的结果却是

    {
        "code": 1,
        "msg": "success",
        "time": 1524036931,
        "data": {
            "list": [
                {
                    "shop_id": 1,
                    "shop_name": "测试商家1",
                    "goodsInfo": [
                        {
                            "id": 1,
                            "name": "测试商品1",
                            "sku": "00000000",
                            "category_id": 0,
                            "business_id": 1,
                            "sale_status": 1,
                            "sort": 0,
                            "price": "0.00",
                            "stock_num": 0,
                            "saled_num": 0,
                            "thumbnail": " ",
                            "create_time": 0,
                            "update_time": 0,
                            "is_delete": 0,
                            "delete_time": null,
                            "priceInfo": {
                                "id": 2,
                                "price": "80.00",
                                "sale_price": "60.00",
                                "host_per": "10.00",
                                "update_time": 0,
                                "is_delete": 0,
                                "delete_time": null
                            }
                        }
                    ]
                },
                {
                    "shop_id": 2,
                    "shop_name": "测试商家2",
                    "goodsInfo": [
                        {
                            "id": 1,
                            "name": "测试商品1",
                            "sku": "00000000",
                            "category_id": 0,
                            "business_id": 1,
                            "sale_status": 1,
                            "sort": 0,
                            "price": "0.00",
                            "stock_num": 0,
                            "saled_num": 0,
                            "thumbnail": " ",
                            "create_time": 0,
                            "update_time": 0,
                            "is_delete": 0,
                            "delete_time": null,
                            "priceInfo": {
                                "id": 2,
                                "price": "80.00",
                                "sale_price": "60.00",
                                "host_per": "10.00",
                                "update_time": 0,
                                "is_delete": 0,
                                "delete_time": null
                            }
                        },
                        {
                            "id": 2,
                            "name": "测试商品2",
                            "sku": "00000000",
                            "category_id": 0,
                            "business_id": 2,
                            "sale_status": 1,
                            "sort": 0,
                            "price": "0.00",
                            "stock_num": 0,
                            "saled_num": 0,
                            "thumbnail": " ",
                            "create_time": 0,
                            "update_time": 0,
                            "is_delete": 0,
                            "delete_time": null,
                            "priceInfo": {
                                "id": 1,
                                "price": "100.00",
                                "sale_price": "99.00",
                                "host_per": "20.00",
                                "update_time": 0,
                                "is_delete": 0,
                                "delete_time": null
                            }
                        },
                        {
                            "id": 3,
                            "name": "测试商品3",
                            "sku": "00000000",
                            "category_id": 0,
                            "business_id": 2,
                            "sale_status": 1,
                            "sort": 0,
                            "price": "0.00",
                            "stock_num": 0,
                            "saled_num": 0,
                            "thumbnail": " ",
                            "create_time": 0,
                            "update_time": 0,
                            "is_delete": 0,
                            "delete_time": null,
                            "priceInfo": {
                                "id": 3,
                                "price": "200.00",
                                "sale_price": "180.00",
                                "host_per": "20.00",
                                "update_time": 0,
                                "is_delete": 0,
                                "delete_time": null
                            }
                        }
                    ]
                }
            ]
        }
    }

    也就是 在商家2里面 所有商品都进去了。实际上商家1有一个商品 ,商家2有2个商品
    求问大佬们这是个什么情况?怎么解决?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部