Ask whether the array assembly suggestion can use some method such as bubble sorting or a better strategy. Thank you. As a result, I have defined a lot of variables and done it with multiple 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": ""
        }
    ]
}

final appearance:

{
    "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": ""
                }
            ]
        }
    ]
}
Php
Mar.06,2021

function getData($data)
{
    $category_list = [];//category_ididfinal_resindex
    $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']] = $indexPP;
            $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;

}

I wonder if this will meet your needs


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