Avatar ·

Error "Cannot use object of type stdClass as array" when working with JSON in PHP

Hello everyone! I'm a beginner in PHP and encountered a strange error that I can't understand. I'm writing a script that gets data from an API in JSON format, decodes it, and tries to output it in a loop. Here's my code:\n\n```php\n$json = file_get_contents('https://api.example.com/users');\n$data = json_decode($json);\n\nforeach ($data as $user) {\n echo $user['name'] . '
';\n echo $user['email'] . '
';\n}\n```\n\nBut when I run it, I get: **Fatal error: Cannot use object of type stdClass as array**. I thought json_decode() returns an array, but apparently that's not the case. What am I doing wrong? How do I properly access data after decoding JSON? I found many discussions online, but I didn't understand a specific solution for my case. I'd be grateful for a detailed explanation with examples!\n\nP.S. If