Avatar ·

Converting PHP Object to Associative Array

Hello everyone! I just started learning PHP and ran into a problem working with objects and arrays.

\n\n

I have a User class with several properties. I create an instance of this class, populate it with data from the database, and then I need to pass this object to a function that expects an associative array as input. The function processes the data and returns JSON for the frontend.

\n\n

Here is a simplified example of my code:

\n\n
<?php\nclass User {\n    public $id;\n    public $name;\n    public $email;\n    public $isActive;\n    \n    public function __construct($id, $name, $email, $isActive = true) {\n        $this->id = $id;\n        $this->name = $name;\n        $this->email = $email;\n        $this->isActive = $isActive;\n    }\n}\n\n$u