php 双重foreach的坑

MrHong
  • 49
$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个商品
求问大佬们这是个什么情况?怎么解决?

回复
阅读 1.6k
4 个回答
$shop_lists = [];
foreach($cardInfo['data']['list'] as $v) {
    if (empty($shop_lists[$v['business_id']])) {
        $shop_lists[$v['business_id']]['shop_id'] = $v['business_id'];
        $shop_lists[$v['business_id']]['shop_name'] = $v['goodsInfo']['name'];
    }

    $shop_lists[$v['business_id']]['goodsInfo'][] = $v['goodsInfo'];
}

$cardInfo['data']['list'] = $shop_lists;

应该是你想要的答案

$cartInfo = collection($cartItems)->toArray();
        $shops =array_column($cartInfo,'shopInfo','business_id');
        $shop_list = [];
        $shop_lists = [];//原来位置
        foreach ($shops as $key => $shop){
            $shop_lists = [];//修改后的位置
            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;

其实就是位置放错了;写的时候是有点感觉不对头;但一直没找到原因

宣传栏