Before knowing how to convert from an object to an array in php, we will know what objects and arrays are in php.
Object and Array are built-in data types of PHP.
Table of Contents
Object in PHP
Definition
An object is an instance of a class. To create an object new keyword is used.
A class is a collection of its constants, variables or properties, and functions or methods.
A class without constants, properties, and methods is an empty class.
We can learn more about the class at: https://www.php.net/manual/en/class.stdclass.php.
Object creation
The easiest way to instantiate an empty object is:
$emptyObject = new stdClass();
The stdClass class is a generic empty class. This class has no methods or default properties.
class stdClass {
}
We can learn more about objects at: https://www.php.net/manual/en/language.types.object.php.
Array in PHP
Definition
An array stores several values in one single variable.
Array creation
An example of an array is:
$colors = ['blue', 'green', 'red'];
We can learn more about arrays in PHP at: https://www.php.net/manual/en/language.types.array.php.
I wrote about How to convert an array to a string in PHP in my previous article at: https://webtechbased.com/how-to-convert-an-array-to-a-string-in-php.
Now I will write about the processes of How to convert an object to an array in PHP.
How to convert an object to an array in PHP
To achieve this, we can follow either of the following 2 processes.
Process 1: Using type casting
With type casting, we can convert the value to a chosen type.
For this first, we will create a class.
Then we will create the object and dump the information using the var_dump function.
Then we convert it to an array via type casting and dump that information using the var_dump function.
var_dump function is used to dump information about a variable. We can We can learn more about this at: https://www.php.net/manual/en/function.var-dump.php.
<?php
class Shapes{
public $shape = '';
public $color = '';
public function __construct($shape, $color){
$this->shape = $shape;
$this->color = $color;
}
}
$newShape = new Shapes('circle', 'green');
echo "Dumping object: <br>";
var_dump($newShape);
// Now Type casting to array
$changeShape = (array)$newShape;
echo "<br>Dumping array: <br>";
var_dump($changeShape);
?>
Here, we will get the output:
Dumping object:
object(Shapes)#1 (2) { [“shape”]=> string(6) “circle” [“color”]=> string(5) “green” }
Dumping array:
array(2) { [“shape”]=> string(6) “circle” [“color”]=> string(5) “green” }
This is one way to convert to an array from an object.
Process 2: Using Json Decode & Json Encode.
json_encode function returns the JSON representation of a given value.
json_decode function is used to decode a JSON string.
We can learn more about json_encode at: https://www.php.net/manual/en/function.json-encode.
We can learn more about json_decode at: https://www.php.net/manual/en/function.json-decode.php.
We are taking the class mentioned above for this process but instead of type casting we will use two functions json_encode and json_decode.
<?php
class Shapes{
public $shape = '';
public $color = '';
public function __construct($shape, $color){
$this->shape = $shape;
$this->color = $color;
}
}
$newShape = new Shapes('circle', 'green');
echo "Dumping object: <br>";
var_dump($newShape);
// Now using json_encode and json_decode to create array
$changeShape = json_decode(json_encode($newShape), true);
echo "<br>Dumping array: <br>";
var_dump($changeShape);
?>
The output of the above code is:
Dumping object:
object(Shapes)#1 (2) { [“shape”]=> string(6) “circle” [“color”]=> string(5) “green” }
Dumping array:
array(2) { [“shape”]=> string(6) “circle” [“color”]=> string(5) “green” }
The second parameter of the json_decode function is associative and when it is true, the function will return associative arrays for JSON objects.