0

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 ?

3
  • php.net/manual/en/function.json-decode.php Commented Feb 4, 2022 at 17:24
  • 2
    Just array_values? Commented Feb 4, 2022 at 17:25
  • 1
    It would be easier to understand if the data in the desired output matched the data in the input Commented Feb 4, 2022 at 17:33

1 Answer 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.

https://3v4l.org/WI4en

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.