How to Convert a PHP Object to an Associative Array
If you want to get a proper explanation of how to convert a PHP object to an associative array, you are in the right place. Just follow the examples.
This snippet will explain the ways of converting a PHP object to an associative array.
Below, you can find efficient ways to meet that goal.
Using json_decode and json_encode
The first method uses the json_decode() and json_encode() functions. json_encode() converts a PHP value into a JSON-encoded string, while json_decode() converts that string back into a PHP variable.
The syntax will look as follows:
php json decode, json encode
<?php
$myArray = json_decode(json_encode($object), true);
?>The following example demonstrates this approach:
php convert object to associative array
<?php
class sample
{
/* Member variables */
var $var1;
var $var2;
function __construct($par1, $par2)
{
$this->var1 = $par1;
$this->var2 = $par2;
}
}
// Creating the object
$myObj = new sample(1000, "second");
echo "Before conversion: \n";
var_dump($myObj);
// Converting object to associative array
$myArray = json_decode(json_encode($myObj), true);
echo "After conversion: \n";
var_dump($myArray);
?>Its outcome looks like this:
php conversion output
Before conversion:
object(sample)#1 (2) {
["var1"]=>
int(1000)
["var2"]=>
string(6) "second"
}
After conversion:
array(2) {
["var1"]=>
int(1000)
["var2"]=>
string(6) "second"
}Note: This method converts all values to basic types. It cannot preserve
DateTimeobjects, resources, or closures. Nested objects will also be converted to arrays.
Casting Object to an Array
The second method is casting the object directly to an array. Type casting explicitly converts a value from one data type to another. In PHP, casting an object to an array converts its properties into array elements.
The syntax is as follows:
php array
<?php
$myArray = (array) $myObj;
?>The example will look like this:
php convert object to array
<?php
class bag
{
/* Member variables */
var $item1;
var $item2;
var $item3;
function __construct($par1, $par2, $par3)
{
$this->item1 = $par1;
$this->item2 = $par2;
$this->item3 = $par3;
}
}
// Create myBag object
$myBag = new bag("Mobile", "Charger", "Cable");
echo "Before conversion : \n";
var_dump($myBag);
// Coverting object to an array
$myBagArray = (array) $myBag;
echo "After conversion : \n";
var_dump($myBagArray);
?>The output will look as follows:
php convert object to array output
Before conversion :
object(bag)#1 (3) {
["item1"]=>
string(6) "Mobile"
["item2"]=>
string(7) "Charger"
["item3"]=>
string(5) "Cable"
}
After conversion :
array(3) {
["item1"]=>
string(6) "Mobile"
["item2"]=>
string(7) "Charger"
["item3"]=>
string(5) "Cable"
}Note: This method only converts public properties. Protected and private properties have their class names prefixed to the property names. It does not recursively convert nested objects. For extracting only public properties without name mangling,
get_object_vars($object)is a direct alternative.
What is an Associative Array
A PHP object is an instance of a class that holds allocated memory for its properties. A standard indexed array stores values using numeric keys. An associative array, however, uses string keys to map values, making it ideal for representing object properties or structured data.