Convert an object to associative array in PHP
An object is an instance of a class. It is simply a specimen of a class and has memory allocated. Array is the data structure that stores one or more similar type of values in a single name but associative array is different from a simple PHP array. An array which contains string index is called associative array. It stores element values in association with key values rather than in linear index order.
Method 1: Using json_decode and json_encode method: The json_decode function accepts JSON encoded string and converts it into a PHP variable on the other hand json_encode returns a JSON encoded string for a given value.
Syntax:
$myArray = json_decode(json_encode($object), true);
Example:
<?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); ?> |
Before conversion:
object(sample)#1 (2) {
["var1"]=>
int(1000)
["var2"]=>
string(6) "second"
}
After conversion:
array(2) {
["var1"]=>
int(1000)
["var2"]=>
string(6) "second"
}
Method 2: Type Casting object to an array: Typecasting is the way to utilize one data type variable into the different data type and it is simply the explicit conversion of a data type. It can convert a PHP object to an array by using typecasting rules supported in PHP.
Syntax:
$myArray = (array) $myObj;
Example:
<?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); ?> |
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"
}
Recommended Posts:
- Convert an Array to an Object in JavaScript
- How to convert an array into object using stdClass() in PHP?
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript?
- How to convert arguments object into an array in JavaScript ?
- How to remove a key and its value from an associative array in PHP ?
- How to loop through an associative array and get the key in PHP?
- Multidimensional Associative Array in PHP
- How to get numeric index of associative array in PHP?
- How to check an array is associative or sequential in PHP?
- Iterate associative array using foreach loop in PHP
- PHP program to add item at the beginning of associative array
- How to access an associative array by integer index in PHP?
- How to remove Objects from Associative Array in JavaScript ?
- How to get a list of associative array keys in JavaScript ?
- How to convert a plain object into ES6 Map using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

