0

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

2
  • What are you using PHP for? Commented Feb 16, 2022 at 15:27
  • I am using third party service in my website with PHP curl, and response they send back to my query is this format. But I want to format it PHP array so that I cant manipulate data. Commented Feb 16, 2022 at 15:30

1 Answer 1

1
$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
Sign up to request clarification or add additional context in comments.

1 Comment

I dont know why but it didnt work. I used early different approach $array=json_decode(json_encode(my_object)); it didnt work also. i think it is similiar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.