Avatar ·

How to convert JSON to a PHP array instead of an object via json_decode()?

Hello everyone! I'm a beginner in PHP and trying to understand JSON. I have a JSON string that I receive from an external API. I use the `json_decode()` function, but it returns an object, and I need an array.\n\nHere is my code:\n```php\n$json = '{"name":"Ivan","age":30,"city":"Moscow"}';\n$data = json_decode($json);\necho $data->name; // Outputs "Ivan" — this is an object\n```\n\nThe problem is that I need to access the data as an array, for example: `$data['name']`. But when I do this, PHP throws an error: "Cannot use object of type stdClass as array".\n\nI tried reading the documentation, but I didn't fully understand how to make `json_decode()` return an array. Please tell me what I need to change in the code? Preferably with an example so I can immediately apply it in my project. Thanks in advance!