把数组中相同的键值进行分组合并,返回新的二维数组

1.初始数据,如下

"data": [
        {
            "id": 1,
            "name": "奥迪",
            "initial": "A",
            "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/1.png"
        },
        {
            "id": 33702,
            "name": "ALPINA",
            "initial": "A",
            "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/33702.png"
        },
        {
            "id": 3,
            "name": "阿尔法·罗密欧",
            "initial": "A",
            "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/3.png"
        },
        {
            "id": 2,
            "name": "阿斯顿·马丁",
            "initial": "A",
            "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/2.png"
        },
        {
            "id": 18,
            "name": "北京",
            "initial": "B",
            "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/18.png"
        },
        {
            "id": 22,
            "name": "布加迪",
            "initial": "B",
            "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/22.png"
        },
        {
            "id": 23,
            "name": "巴博斯",
            "initial": "B",
            "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/23.png"
        },
        {
            "id": 17,
            "name": "奔腾",
            "initial": "B",
            "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/17.png"
        },
        {
            "id": 16,
            "name": "北汽绅宝",
            "initial": "B",
            "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/16.png"
        },
        {
            "id": 15,
            "name": "保时捷",
            "initial": "B",
            "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/15.png"
        }
    ];

2.核心处理数组分组函数

/**
 * @param $data 一维数组
 * @param $field 字段
 * @return array 新二维数组
 */
function handleDataGroup($data,$field){
    $newdata =[];
    foreach($data as $k=>$v){
        $newdata[$v[$field]][] = $v;
    }
    return $newdata;
}

3.新数组结果

"data": {
        "A": [
            {
                "id": 1,
                "name": "奥迪",
                "initial": "A",
                "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/1.png"
            },
            {
                "id": 33702,
                "name": "ALPINA",
                "initial": "A",
                "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/33702.png"
            },
            {
                "id": 3,
                "name": "阿尔法·罗密欧",
                "initial": "A",
                "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/3.png"
            },
            {
                "id": 2,
                "name": "阿斯顿·马丁",
                "initial": "A",
                "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/2.png"
            }
        ],
        "B": [
            {
                "id": 18,
                "name": "北京",
                "initial": "B",
                "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/18.png"
            },
            {
                "id": 22,
                "name": "布加迪",
                "initial": "B",
                "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/22.png"
            },
            {
                "id": 23,
                "name": "巴博斯",
                "initial": "B",
                "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/23.png"
            },
            {
                "id": 17,
                "name": "奔腾",
                "initial": "B",
                "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/17.png"
            },
            {
                "id": 16,
                "name": "北汽绅宝",
                "initial": "B",
                "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/16.png"
            },
            {
                "id": 15,
                "name": "保时捷",
                "initial": "B",
                "logo": "http:\/\/pic1.jisuapi.cn\/car\/static\/images\/logo\/300\/15.png"
            }
        ]
Last modification:July 13th, 2019 at 04:57 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment