The pit of php double foreach

$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
                    }
                }
            }
        ]
    }
}

want to transform the array structure to

{
    "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}
                ]
            }
        ]
    }
}

so you have the following code

        $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;

but the result is

{
    "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
                        }
                    }
                ]
            }
        ]
    }
}

that is, in Merchant 2, all the goods have gone in. In fact, merchant 1 has a commodity, merchant 2 has two goods
ask the bosses what is the situation? How to solve it?

Php
Mar.04,2021

I wiped my screen with my hands!


the code was frightened by your picture before I could see it


$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;

should be the answer you want


$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;

actually put it in the wrong place; it didn't feel right when I wrote it, but I never found the reason

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37d55-2c0a9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37d55-2c0a9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?