[英]multidimensional array json_decode (first level)
I have a JSON file like this: 我有一个像这样的JSON文件:
{
"numeric1": {
"id": "numeric1",
"name": "alphanumeric1",
"key": "alphanumeric2",
"expire": "alphanumeric3",
"status": true,
"ads": true
},
etc...
}
with (etc...) I mean that matrix is repeated more times. 与(等)我的意思是矩阵重复了更多次。 I parse it with PHP using:
我用PHP解析它:
$allowed = json_decode(file_get_contents("allowed.json"), true);
Then I get an array like: 然后我得到一个像这样的数组:
Array
(
[0] => Array
(
[id] => numeric1
[name] => alphanumeric1
[key] => alphanumeric2
[expire] => alphanumeric3
[status] => 1
[ads] => 1
)
etc....
)
So I lose the first level of associative keys, I have 所以我失去了第一级的关联密钥,
[0] => Arrayinstead of
["numeric1"] => Array[“ numeric1”] =>数组
How can I keep the first level of my JSON array? 如何保留JSON数组的第一级? Thanks.
谢谢。
Try this: 尝试这个:
$allowed = (array) json_decode(file_get_contents("allowed.json"));
So instead of directly parsing the JSON as an array (by specifying second parameter of json_decode), first get the object that will preserve the key, then cast as an array. 因此,不是直接将JSON解析为数组(通过指定json_decode的第二个参数),而是首先获取将保留密钥的对象,然后将其转换为数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.