Convert stdclass object to array PHP
In this post, you will learn how to convert stdclass object to array using the PHP programming language.
The stdClass is the empty class in PHP, which is used to cast other types to an object. The stdClass is not the base class of objects. If an object is converted to an object, it is not modified. But, if an object type is converted/type-casted an instance of stdClass is created if it is not NULL. If it is NULL, the new instance will be empty. An array is a collection of key/value pairs. In programming, sometimes we need to convert stdclass objects to arrays. It is easy to convert a stdclass object to an array if both the arrays and objects are one-dimensional, but it might be a little tricky if using multidimensional arrays and objects.
Convert stdClass objects to array
PHP provides stdClass as a generic empty class which is useful for adding properties dynamically and casting. It is useful in dynamic objects. Suppose we have the following stdClass objects.
#define stdObjects to store employee details
$obj = new stdClass;
$obj->name = "Priska";
$obj->position = "Data Administrator"
$obj->age = 26;
$obj->experience = 3;
The following PHP code converts the above defined objects into an array.
function convert_object_to_array($data) {
if (is_object($data)) {
$data = get_object_vars($data);
}
if (is_array($data)) {
return array_map(__FUNCTION__, $data);
}
else {
return $data;
}
}
#define stdObjects to store employee details
$obj = new stdClass;
$obj->name = "Priska";
$obj->position = "Data Administrator";
$obj->age = 26;
$obj->experience = 3;
print_r($obj);
$emp = convert_object_to_array($obj);
print_r($emp);
Output of the above code:
stdClass Object
(
[name] => Priska
[position] => Data Administrator
[age] => 26
[experience] => 3
)
Array
(
[name] => Priska
[position] => Data Administrator
[age] => 26
[experience] => 3
)
Function to Convert stdClass Objects to Multidimensional Arrays
In the given example, we have converted the stdClass objects to multidimensional arrays.
function convert_object_to_array($data) {
if(is_object($data)) {
// Get the properties of the given object
$data = get_object_vars($data);
}
if(is_array($data)) {
//Return array converted to object
return array_map(__FUNCTION__, $data);
}
else {
// Return array
return $data;
}
}
#define stdObjects to store employee details
$obj = new stdClass;
$obj->name = "Priska";
$obj->position = "Data Administrator";
$obj->age = 26;
$obj->experience = 3;
$obj->address->houseno = "K-92 A, Bank Street";
$obj->address->location = "Southcamp";
$obj->address->city = "Banglore";
print_r($obj);
$emp = convert_object_to_array($obj);
print_r($emp);
Output of the above code:
stdClass Object
(
[name] => Priska
[position] => Data Administrator
[age] => 26
[experience] => 3
[address] => stdClass Object
(
[houseno] => K-92 A, Bank Street
[location] => Southcamp
[city] => Banglore
)
)
Array
(
[name] => Priska
[position] => Data Administrator
[age] => 26
[experience] => 3
[address] => Array
(
[houseno] => K-92 A, Bank Street
[location] => Southcamp
[city] => Banglore
)
)
Related Articles
PHP reverse a string without predefined functionPHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
PHP random quote generator
PHP calculate percentage of total
PHP sanitize input for MySQL
Display PDF using an AJAX call
How to fetch data from database in php and display in pdf
How to read CSV file in PHP and store in MySQL
How to create a doc file using PHP
PHP SplFileObject Examples
How to Upload a File in PHP
Sending HTML form data to an email address