Home > OS >  How to create array from object?
How to create array from object?

Time:02-17

I dont know what to call this array-looking object. But When I try to access elements in Php, I cant.

    {
    "message":"authenticated", 
      "data": {"id":713,"name":"Jamal","email":"[email protected]","role":"user"}
    }

I want to access the elements like this. $array['message']=> expecting the outcome to be "authenticated". please help

CodePudding user response:

$JSON = file_get_contents('UserData.json'); // Parsing JSON from file. You can use your own way.
$Array = json_decode($JSON, true); // JSON (string) to Array
echo $Array["message"]; // Output: authenticated
echo $Array["data"]["id"]; // Output: 713
  • Related