鍍金池/ 問(wèn)答/PHP/ 求數(shù)組拼裝建議 能否用上什么冒泡排序之類的方法 或者 更好的策略 謝謝 結(jié)果是

求數(shù)組拼裝建議 能否用上什么冒泡排序之類的方法 或者 更好的策略 謝謝 結(jié)果是我定義了很多變量多個(gè)foreach 完成的

{
    "head": {
        "code": 200,
        "msg": "ok"
    },
    "body": [
        {
            "id": "80761",
            "name": "這是一個(gè)商品型的測(cè)試",
            "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": "這是一個(gè)商品型的測(cè)試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": "這是一個(gè)商品型的測(cè)試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": "這是一個(gè)商品型的測(cè)試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": "這是一個(gè)商品型的測(cè)試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": "這是一個(gè)商品型的測(cè)試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": "這是一個(gè)商品型的測(cè)試",
                    "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": "這是一個(gè)商品型的測(cè)試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": "這是一個(gè)商品型的測(cè)試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": "這是一個(gè)商品型的測(cè)試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": "這是一個(gè)商品型的測(cè)試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": "這是一個(gè)商品型的測(cè)試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": "選材類"
                }
            ]
        }
    ]
}
回答
編輯回答
吢丕
$new_data   = [];  // 收集分類相同的數(shù)據(jù)
        $cate_goods = [];  // 收集分類和商品信息--最終數(shù)據(jù)
        $cate       = [];  // 拼接分類 將分類單獨(dú)提出來(lái)
        if ( $result )
        {

            foreach ( $result as $key => $value ) 
            {
                $value['img_url'] = $this->img_url.$value['img_url'];
                $new_data[$value['category_id']][]  = $value;
            }

            // 將分類名稱單獨(dú)提出來(lái)
            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;
                    }
                }
            }
        }
2018年1月13日 18:06
編輯回答
傲寒
$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;
    
}
2017年2月25日 21:43
編輯回答
懷中人
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;

}

不知道這個(gè)能不能滿足你的需求

2017年4月9日 10:02