Convert stdClass object to associative array in php

advertisements

I need to convert this array

Array (
[0] => stdClass Object
     ( [title] => primo )
[1] => stdClass Object
     ( [title] => secondo ))

to

Array (
[primo] => primo
[secondo] => secondo )

Tried different options, including typecast, still not found the correct solution


Use json_encode() and json_decode()

$arr = json_decode(json_encode($yourObject), TRUE);

json_decode() 's second parameter is set to TRUE.

Function definition:

mixed json_decode ( string $json [, bool $assoc = false [, int $depth > = 512 [, int $options = 0 ]]] )

That will convert your object into an associative array.