phparraysassociative

How to create array from object?


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":"abdurasulovsokhib@gmail.com","role":"user"}
    }

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


Solution

  • $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