简体   繁体   中英

Iterate and group array of objects in PHP

I have the following MySQL table:

+----+----------+--------+------+
| id | brand    | model  | year |
+----+----------+--------+------+
| 1  | audi     | tt     | 2014 |
+----+----------+--------+------+
| 2  | audi     | a4     | 2015 |
+----+----------+--------+------+
| 3  | mercedes | aclass | 2014 |
+----+----------+--------+------+
| 4  | mercedes | cclass | 2017 |
+----+----------+--------+------+

I am using MYSQL in PHP and writing a query to get this data:

$carsQuery = SELECT * FROM cars_table ORDER BY model ASC;

After the query is executed I am using PHP to try an achieve the following JSON Object output:

{
    "car_models": [
        {
            "id": "audi",
            "name": "Audi",
            "cars": [
                {
                    "id": "1",
                    "model": "tt",
                    "year": "2014",
                    "brand": "audi"
                    },
                {
                    "id": "2",
                    "model": "a4",
                    "year": "2015",
                    "brand": "audi"
                }
            ]
        },
        {
            "id": "mercedes",
            "name": "Mercedes",
            "cars": [
                {
                    "id": "3",
                    "model": "aclass",
                    "year": "2014",
                    "brand": "mercedes"
                    },
                    {
                    "id": "4",
                    "model": "cclass",
                    "year": "2017",
                    "brand": "mercedes"
                    }
            ]
        }
    ]
}

The following snippet is the PHP code I am using to achieve this:

foreach($carsQuery as $carQuery){ 
        $cars[$carQuery->brand][] = $carQuery;
    }

return array( 
        'car_models' => $carQuery
        ); 

The result is being grouped however I cannot get to the point of adding the other fields in order to achieve the JSON object I want.

Adding the other fields is mostly just a matter of defining them in the array you're building.

foreach ($carsQuery as $carQuery) {
    $cars[$carQuery->brand]['id'] = $carQuery->brand;
    $cars[$carQuery->brand]['name'] = ucfirst($carQuery->brand);
    $cars[$carQuery->brand]['cars'][] = $carQuery;
}

'id' and 'name' will be overwritten with the same value for multiple records of the same brand.

You may need to use some other approach than ucfirst to format the 'name' field, as some values may have different capitalization.

You should also use array_values to create your final array, in order to replace the string keys with numeric ones.

return array(
    'car_models' => array_values($cars)
);

That will ensure that car_models is an array of objects in your final JSON, rather than an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM