Notification texts go here Contact Us Buy Now!

Laravel - How to convert Stdclass object to Array

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);

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.