简体   繁体   English

将 Json 解码为 php 中的数组?

[英]Decode Json to array in php?

I have a json object in this structure:我在这个结构中有一个json object :

 { "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:我希望从这个 object 中得到一个数组,例如:

 [{ "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?有没有办法达到这个结果?

You can use PHP's built-in json_decode function to decode your JSON payload.您可以使用 PHP 的内置json_decode function 来解码您的 JSON 有效负载。

This will return you an stdClass object whose properties will contain your 2nd level objects.这将为您返回一个 stdClass object,其属性将包含您的第二级对象。

Casting this object to an array will preserve your 2nd level objects.将此 object 转换为数组将保留您的第二级对象。

If you need to convert the result back to JSON, use the json_encode method.如果您需要将结果转换回 JSON,请使用json_encode方法。

https://3v4l.org/WI4en https://3v4l.org/WI4en

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

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