简体   繁体   English

在PHP中迭代和分组对象数组

[英]Iterate and group array of objects in PHP

I have the following MySQL table: 我有以下MySQL表:

+----+----------+--------+------+
| 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: 我在PHP中使用MYSQL并编写查询以获取此数据:

$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: 执行查询后,我正在使用PHP尝试实现以下JSON对象输出:

{
    "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: 以下代码段是我用来实现此目的的PHP代码:

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. 结果进行了分组,但是我无法达到添加其他字段以实现所需的JSON对象的目的。

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. 对于同一品牌的多个记录,“ id”和“ name”将被覆盖相同的值。

You may need to use some other approach than ucfirst to format the 'name' field, as some values may have different capitalization. 您可能需要使用ucfirst以外的其他方法来格式化“名称”字段,因为某些值可能具有不同的大小写。

You should also use array_values to create your final array, in order to replace the string keys with numeric ones. 您还应该使用array_values创建最终数组,以便用数字键替换字符串键。

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. 这将确保car_models是最终JSON中的对象数组,而不是对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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