How to get multiple keys in JSON with PHP?

Asked

Viewed 123 times

0

How to obtain data with foreach through the JSON of that kind:

{
    "12": {
        "0": {
            "9678": {
                "920": {
                    "224": {
                        "657": "José da Silva"
                    }
                }
            }
        },
        "1": {
            "0512": {
                "987/21": {
                    "233": {
                        "652": "Maria Silva"
                    }
                }
            }
        }
    }
}
  • I don’t understand. You just want a simple list with the names "José da Silva" and "Maria Silva"?

1 answer

0

You can use PHP’s native "json_decode" function

$json = json_decode('{"12":{"0":{"9678":{"920":{"224":{"657":"José da Silva"}}}},"1":{"0512":{"987/21":{"233":{"652":"Maria Silva"}}}}}}', true);

foreach ($json as $obj) {
    var_dump($obj);
}

Edit

I added a parameter true json_encode to return the data in array format and not object format.

  • I need to store these values within the database. Therefore, I need to get them separately.

  • I made a small correction in the code, added a parameter true the function so that you can return the data in array format and not object as it was initially. About adding the data separately by your json we have a problem, you do not use a logical sequence for the data, can not use an increment to recover the data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.