I have a JSON file like this:
{
"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:
$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
How can I keep the first level of my JSON array? 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.