'Decode Json to array in php?
I have a json object in this structure :
{
"a": {
"a": "10",
"q": "dumb text"
},
"b": {
"a": "23:00",
"q": "dumb text"
},
"c": {
"a": null,
"q": "dumb text"
}
}
And i'm looking to get an array from this object like :
[{ "a": "10", "q": "dumb text 1" }, { "a": "20", "q": "dumb text 2" },{ "a": "30", "q": "dumb text 3" }]
Is there a way to achieve this result ?
Solution 1:[1]
You can use PHP's built-in json_decode function to decode your JSON payload.
This will return you an stdClass object whose properties will contain your 2nd level objects.
Casting this object to an array will preserve your 2nd level objects.
If you need to convert the result back to JSON, use the json_encode method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nicolas Hedger |
