In this article, we are going to demonstrate how to convert a PHP object to an associative array. You can find effective approaches to achieving that goal.
Methods to Convert the PHP Object into a PHP Associative Array
Several methods for converting objects to associative arrays are listed below:
1: Using get_object_vars
In PHP, an object can be converted into an associative array using the get_object_vars() function. The get object var() function retrieves the specified object’s defined properties and returns an associative array of them.
Syntax: The syntax of the get_object_vars() function in PHP is given as:
The function takes an object as its argument and returns an associative array containing the object’s defined properties and their values.
Example
Let’s consider an example that demonstrates the working of the get_object_vars() function in PHP.
// Create an object
$myObj = new stdClass();
$myObj->name = 'John';
$myObj->age = 26;
// Get the object properties as an associative array
$associative_Array = get_object_vars($myObj);
// Print the associative array
print_r($associative_Array);
?>
The above code creates a new object, sets its properties, extracts those properties as an associative array using the get_object_vars() function, and then prints the array using print_r().
Output
2: Using Type Casting
A technique for changing a variable’s data type is type casting. It’s an explicit conversion of the data type. By converting a PHP object to a PHP array data type, one can use PHP to convert the PHP object into a PHP associative array.
Syntax: The syntax for type casting an object in PHP is given as:
$myObj->var1 = value1;
$myObj->var2 = value2;
$associative_Array = (array) $myObj;
Example
Let’s consider an example that demonstrates the working of the type-casting method in PHP.
The above code creates an object called $myObj with name and age properties. It then uses typecasting to convert the object to an associative array and finally prints out the contents of the array using print_r().
Output
3: Using json_encode and json_decode
You can also use json_enecode and json_decode functions for converting an object in PHP into an associative array. The json_encode accepts a PHP variable as its input and returns a JSON-encoded string that represents the input value. While json_decode accepts a JSON-encoded string as its input and returns a PHP variable based on the contents of the input string.
Syntax: The syntax of the json_encode and json_decode functions in PHP is given as:
In the above syntax, the $object variable is being encoded using json_encode, and the resulting JSON string is being decoded back into a PHP associative array using json_decode.
The true parameter passed as the second argument to json_decode shows that the resulting array should be an associative array rather than an object.
Example
Take a look at the example below to get more clarity:
// Create a PHP object
$obj = new stdClass();
$obj->name = 'John';
$obj->age = 26;
// Convert object to JSON string
$jsonStr = json_encode($obj);
// Convert JSON string to associative array
$assocArr = json_decode($jsonStr, true);
// Output associative array
print_r($assocArr);
?>
The above code creates a PHP object with two properties (name and age), converts the object to a JSON string using json_encode(), then converts the JSON string back to an associative array using json_decode() with the second parameter set to true and finally, it prints the resulting associative array using print_r().
Output
Conclusion
PHP uses associative arrays, which pair element values to key values instead of in linear index order. Associative arrays have string indexes. There are several ways to convert an object in PHP into an associative array, including using get_object_vars(), typecasting, json_encode(), and json_decode() functions. Each method has its own syntax and approach for achieving the goal.