Avatar ·

How to convert a PHP object to an associative array?

Hello everyone! I'm new to PHP and ran into this problem. I have an object that is returned from a database via PDO or from some API (e.g., JSON decoding). I need to work with it as a regular array to use functions like array_keys(), array_values() or simply iterate over keys in a foreach loop. Here's an example: I get user data as a stdClass object. ```php $user = new stdClass(); $user->name = 'Ivan'; $user->email = 'ivan@example.com'; $user->age = 30; ``` Now I want to convert this object into an array like: ```php $userArray = [ 'name' => 'Ivan', 'email' => 'ivan@example.com', 'age' => 30 ]; ``` I tried just assigning it to a variable, but that doesn't work. I know there are some functions in PHP for this, but