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
Method 1: Using json_decode and json_encode method: The json_decode function accepts JSON encoded string and converts it into a PHP variable on the other hand json_encode returns a JSON encoded string for a given value. Syntax: $myArray = json_decode(json_encode($object), true);
The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With