求数组拼装建议 能否用上什么冒泡排序之类的方法 或者 更好的策略 谢谢 结果是我定义了很多变量多个foreach 完成的

{
    "head": {
        "code": 200,
        "msg": "ok"
    },
    "body": [
        {
            "id": "80761",
            "name": "这是一个商品型的测试",
            "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
            "specification": "607,608",
            "attributes": "87,88",
            "unit": "斤",
            "label": "0",
            "type": "1",
            "inventory": "-1",
            "category_id": "8689",
            "category_name": "",
            "c_name": "香锅类"
        },
        {
            "id": "80762",
            "name": "这是一个商品型的测试2",
            "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
            "specification": "609,610",
            "attributes": "89,90",
            "unit": "份",
            "label": "0",
            "type": "2",
            "inventory": "-1",
            "category_id": "8689",
            "category_name": "",
            "c_name": "香锅类"
        },
        {
            "id": "80763",
            "name": "这是一个商品型的测试2",
            "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
            "specification": "611,612",
            "attributes": "91,92",
            "unit": "份",
            "label": "0",
            "type": "2",
            "inventory": "-1",
            "category_id": "8689",
            "category_name": "",
            "c_name": "香锅类"
        },
        {
            "id": "80764",
            "name": "这是一个商品型的测试2",
            "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
            "specification": "613,614",
            "attributes": "93,94",
            "unit": "份",
            "label": "0",
            "type": "2",
            "inventory": "-1",
            "category_id": "8690",
            "category_name": "",
            "c_name": "选材类"
        },
        {
            "id": "80765",
            "name": "这是一个商品型的测试2",
            "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
            "specification": "615,616",
            "attributes": "95,96",
            "unit": "份",
            "label": "0",
            "type": "2",
            "inventory": "-1",
            "category_id": "8690",
            "category_name": "",
            "c_name": "选材类"
        },
        {
            "id": "80766",
            "name": "这是一个商品型的测试2",
            "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
            "specification": "617,618",
            "attributes": "97,98",
            "unit": "份",
            "label": "0",
            "type": "2",
            "inventory": "-1",
            "category_id": "8690",
            "category_name": "",
            "c_name": "选材类"
        }
    ]
}

最终的样子:

{
    "head": {
        "code": 403,
        "msg": "门店不存在"
    },
    "body": [
        {
            "cate_id": "8689",
            "cate_name": "香锅类",
            "info": [
                {
                    "id": "80761",
                    "name": "这是一个商品型的测试",
                    "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
                    "specification": "607,608",
                    "attributes": "87,88",
                    "unit": "斤",
                    "label": "0",
                    "type": "1",
                    "inventory": "-1",
                    "category_id": "8689",
                    "category_name": "",
                    "c_name": "香锅类"
                },
                {
                    "id": "80762",
                    "name": "这是一个商品型的测试2",
                    "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
                    "specification": "609,610",
                    "attributes": "89,90",
                    "unit": "份",
                    "label": "0",
                    "type": "2",
                    "inventory": "-1",
                    "category_id": "8689",
                    "category_name": "",
                    "c_name": "香锅类"
                },
                {
                    "id": "80763",
                    "name": "这是一个商品型的测试2",
                    "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
                    "specification": "611,612",
                    "attributes": "91,92",
                    "unit": "份",
                    "label": "0",
                    "type": "2",
                    "inventory": "-1",
                    "category_id": "8689",
                    "category_name": "",
                    "c_name": "香锅类"
                }
            ]
        },
        {
            "cate_id": "8690",
            "cate_name": "选材类",
            "info": [
                {
                    "id": "80764",
                    "name": "这是一个商品型的测试2",
                    "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
                    "specification": "613,614",
                    "attributes": "93,94",
                    "unit": "份",
                    "label": "0",
                    "type": "2",
                    "inventory": "-1",
                    "category_id": "8690",
                    "category_name": "",
                    "c_name": "选材类"
                },
                {
                    "id": "80765",
                    "name": "这是一个商品型的测试2",
                    "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
                    "specification": "615,616",
                    "attributes": "95,96",
                    "unit": "份",
                    "label": "0",
                    "type": "2",
                    "inventory": "-1",
                    "category_id": "8690",
                    "category_name": "",
                    "c_name": "选材类"
                },
                {
                    "id": "80766",
                    "name": "这是一个商品型的测试2",
                    "img_url": "/uploads/zhongshan/2/216593787b72633ab83a44992c511e5a.jpeg",
                    "specification": "617,618",
                    "attributes": "97,98",
                    "unit": "份",
                    "label": "0",
                    "type": "2",
                    "inventory": "-1",
                    "category_id": "8690",
                    "category_name": "",
                    "c_name": "选材类"
                }
            ]
        }
    ]
}
蝴蝶刀刀
浏览 427回答 3
3回答

牧羊人nacy

$old_data = $old['body']; foreach($old_data as $v){ if (!array_key_exists($v['category_id'],$new)){ $new[$v['category_id']]['cate_id'] = $v['category_id']; $new[$v['category_id']]['cate_name']= $v['c_name']; } $new[$v['category_id']]['boby'][] = $v; }

小怪兽爱吃肉

$new_data = []; // 收集分类相同的数据 $cate_goods = []; // 收集分类和商品信息--最终数据 $cate = []; // 拼接分类 将分类单独提出来 if ( $result ) { foreach ( $result as $key => $value ) { $value['img_url'] = $this->img_url.$value['img_url']; $new_data[$value['category_id']][] = $value; } // 将分类名称单独提出来 if ( $new_data ) { foreach ( $new_data as $k => $v ) { if ( $v ) { $cate['cate_id'] = $v[0]['category_id']; $cate['cate_name'] = $v[0]['c_name']; $cate['info'] = $v; $cate_goods[] = $cate; } } } }
打开App,查看更多内容
随时随地看视频慕课网APP