1

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.

1
  • Did you choose that JSON structure or is it an external constraint? If the former then you might reconsider the structure. If the latter then you should align the sorting of the query output with the data hierarchy in your JSON. Commented Mar 16, 2018 at 18:30

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Many thanks for your reply, that worked perfectly fine. The only issue, the JSON output is not sorted how I want. How can I sort the objects with the brand name?
Add it to the order by clause in your query. ...ORDER BY brand, model or actually, if you want it like your example, brand, id
I already order by brand, however the model in the car s array are not alphabetical
@Jojo I noticed that after I commented before. I edited the previous comment. With your example data, you should be able to get your example output with either ORDER BY brand, id or ORDER BY brand, year.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.