Tags
PHP
Asked 1 years ago
26 Apr 2023
Views 328
shyam

shyam posted

how to convert stdclass to array?

how to convert stdclass to array?
joomler

joomler
answered May 1 '23 00:00

at PHP, you can use the json_encode() and json_decode() functions to convert an object of stdClass type to an array . Here's an example code snippet:



// Create a stdClass object
$obj = new stdClass();
$obj->name = "John Doe";
$obj->age = 30;

// Convert the object to a JSON string
$json_str = json_encode($obj);

// Convert the JSON string to an array
$arr = json_decode($json_str, true);

// Print the array
print_r($arr);

In this example, we create a stdClass object with two properties (name and age). We then convert the object to a JSON string using the json_encode() function, and convert the JSON string to an array using the json_decode() function with the second parameter set to true.

The resulting array should have the same keys and values as the original stdClass object.
Post Answer