Вопрос

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] => Array
instead 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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top