While your API uses objects to store data, your code relies on arrays. To bridge this gap, you seek a quick way to convert objects into arrays.
The simplest method is to typecast the object into an array:
$array = (array) $yourObject;
By doing this, the object's properties become the keys of the resulting array.
Example:
$object = new StdClass; $object->foo = 1; $object->bar = 2; var_dump((array) $object);
Output:
array(2) { 'foo' => int(1) 'bar' => int(2) }
Important Notes:
For a more detailed blog post on PHP object-to-array conversion, refer to:
The above is the detailed content of How Can I Quickly Convert PHP Objects to Associative Arrays?. For more information, please follow other related articles on the PHP Chinese website!