求数组拼装建议 能否用上什么冒泡排序之类的方法 或者 更好的策略 谢谢 结果是我定义了很多变量多个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": "选材类"
                }
            ]
        }
    ]
}
阅读 2.1k
3 个回答
$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;
    
}
function getData($data)
{
    $category_list = [];//记录不同category_id和该id在final_res中的index
    $index = 0;//不同的category_id 生成不同的index
    $final_res = [];
    foreach ($data['body'] as $key => $value)
    {

        if (!isset($category_list[$value['category_id']])) {
            $category_list[$value['category_id']] = $index++;
            $final_res[$category_list[$value['category_id']]] = [
                'cate_id'=>$value['category_id'],
                'cate_name'=>$value['c_name']
            ];

        }
        $final_res[$category_list[$value['category_id']]]['info'][] = $value;

        
    }
    return $final_res;

}

不知道这个能不能满足你的需求

$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;
                    }
                }
            }
        }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏