If you want to convert a Stdclass object into an array in Laravel, here are several methods you can use:
$data->toArray();
This method will convert the entire Stdclass object into an array. If you only want to convert a specific property of the object, you can use the following syntax:
$data->property->toArray();
To convert every object within an array to an array, you can use the following code:
$data->map(function($i) {
return (array)$i;
})->toArray();
Another method to convert a Stdclass object to an array is by using json_decode()
and json_encode()
functions like so:
$result = json_decode(json_encode($data, true));
If you want to convert all nested properties to an array, you can add a true
parameter to the second argument of json_encode()
:
$result = json_decode(json_encode($data, true), true);
Additionally, you can use array_map()
function to convert a Stdclass object to an array:
$data = array_map(function($i) {
return (array)$i;
}, $data);