How to Convert PHP Object to Associative Array
-
Use the
array
Keyword to Typecast theStdClass
’s Object to Convert Into an Associative Array in PHP -
Use the
StdClass
’s Object Inside of a User-Defined Class to Convert the Object Into an Associative Array in PHP -
Use the
json_encode()
andjson_decode()
Functions to Convert the Object Into an Associative Array in PHP
We will introduce a method to convert the PHP object to an associative array typecasting the objects of StdClass
using the array
keyword. We will use the var_dump()
function to display the associative array.
The second approach demonstrates another method to convert the PHP object into an associative array creating a StdClass
in the constructor of a user-defined class. We will convert the object into the associative array as in the first method using the array
keyword. This method follows the object-oriented approach.
We will also introduce another method to convert the object into associative array in PHP using the json_encode()
and json_decode()
functions. We will use the StdClass
to create the object and the dynamic properties.
Use the array
Keyword to Typecast the StdClass
’s Object to Convert Into an Associative Array in PHP
We can create the StdClass
empty class to create an object in PHP and use the object to create properties. The object of the class can directly access the properties. It can also create dynamic properties for the class. We can use the array
object to typecast the object into an array. The var_dump()
function dumps the information about the array’s type and values.
For example, create an object $object
of the StdClass
using the new
operator. Create two properties using the $object
named car1
and car2
. Assign the properties with the values porsche
and bugatti
. Use the array
keyword to typecast the $object
variable. Wrap the array
keyword with parenthesis before the $object
variable and dump the value using the var_dump()
function. The example below converts the objects into an associative array, as shown in the output section. It shows the key and value pairs for each element of the array.
Example Code:
#php 7.x
<?php
$object = new StdClass;
$object->car1 = "porsche";
$object->car2 = "bugatti";
var_dump( (array) $object );
?>
Output:
array(2) { ["car1"]=> string(7) "porsche" ["car2"]=> string(7) "bugatti" }
Use the StdClass
’s Object Inside of a User-Defined Class to Convert the Object Into an Associative Array in PHP
We can create an instance of the StdClass
in the class’s constructor and use the array
keyword to convert the classes’ object to an associative array. We can create a class and define some properties of it. The constructor of the class initializes the properties of the class to specific values. We can create an object of the StdClass
and assign it with one of the properties of the class. The new
operator, along with the class, invokes the constructor. We can use the array
keyword right before the invocation to convert the object of the class to an associative array.
For example, create a class Motorcycle
. Create three class properties with the private
access modifier as $name
, $color
, and $type
. Create a constructor of the class and inside the constructor initialize the values of the properties. Write Husky
for the name
, white
for color
and create an object of the StdClass
for type
. Use the $this
keyword to initialize the properties. Outside the class, invoke the Motorcycle
class and use the array
keyword to typecast before invoking. Use the var_dump()
function to dump the information about the typecasted array.
Example Code:
#php 7.x
class Motorcycle{
private $name;
private $color;
private $type;
public function __construct(){
$this->name = "Husky";
$this->color = "white";
$this->type = new StdClass;
}
}
var_dump( (array) new Motorcycle );
Output:
array(3) { ["Motorcyclename"]=> string(5) "Husky" ["Motorcyclecolor"]=> string(5) "white" ["Motorcycletype"]=> object(stdClass)#2 (0) { } }
Use the json_encode()
and json_decode()
Functions to Convert the Object Into an Associative Array in PHP
The json_encode()
function encodes a value to the JSON object and the json_decode()
function converts the JSON object to the PHP object. The boolean value, the second parameter of the json_decode()
function, indicates what the JSON object should be converted. The true
value will convert the JSON object to an associative array, while the false
value will convert it into a PHP object.
For example, create an object of the StdClass
and assign it to the $object
variable. Name two values of the object property as Mustang
and Manang
and store them in place1
and place2
variable. Use the json_encode()
function on the $object
variable and store the value in $json
variable. Then, use the json_decode()
function on the $json
variable and use the boolean value true
as the second parameter. Store the value in $array
variable. Dump the variable using the var_dump()
function.
In the example below, the variable $object
holds the StdClass
object. The json_ecode()
function converts the object into the JSON string. The json_decode()
function converts the JSON string into the associative array.
Example Code:
#php 7.x
<?php
$object = new StdClass;
$object->place1 = "Mustang";
$object->place2 = "Manang";
$json= json_encode($object);
$array = json_decode($json, true);
var_dump($array);
?>
Output:
array(2) { ["place1"]=> string(7) "Mustang" ["place2"]=> string(6) "Manang" }
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
LinkedIn