18
Nov
Learn how to convert php stdClass to array
If you need to convert php stdClass to array, there is a lazy one-liner method.
In one liner we are using the JSON methods if you’re willing to lose a tiny bit of performance. Here you need to encode stdClass with json_encode and after that decode it using json_decode.
Converting an array/stdClass -> stdClass
$stdClass = json_decode(json_encode($std_class));
Converting an array/stdClass -> array
In the json_decode manual specifies second parameter.
assoc When TRUE, returned objects will be converted into associative arrays.
Hence, it will convert entire thing to array.
$array = json_decode(json_encode($std_class), true);