
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Convert object to an array in PHP.
In a PHP application, we are working with data in various formats such as string, array, objects or more...In a real-time application, we may need to read a php object result in the form of an associative array to get the desired output.
So we will discuss here how to transform a php object to an associative array in PHP.
Let's explain what is an object and associative array in PHP? An object is an instance of a class meaning that from one class you can create many objects. It is simply a specimen of a class and has memory allocated. While on the other hand an array which consists of string as an index is called associative array. It contains a key-value pair in it, in which values are associated with their respective keys.
Let's now discuss various methods of converting the object to an array.
Method1:
Utilizing json_decode and json_encode technique:
Initially json_encode() function returns a JSON encoded string for a given value.The json_decode() function changes over it into a PHP array.
Example:
<?php class student { public function __construct($firstname, $lastname) { $this->firstname = $firstname; $this->lastname = $lastname; } } $myObj = new student("Alex", "Stokes"); echo "Before conversion:".'</br>'; var_dump($myObj); $myArray = json_decode(json_encode($myObj), true); echo "After conversion:".'</br>'; var_dump($myArray); ?>
Output:
Before conversion: object(student)#1 (2) { ["firstname"]=> string(4) "Alex" ["lastname"]=> string(6) "Stokes" } After conversion: array(2) { ["firstname"]=> string(4) "Alex" ["lastname"]=> string(6) "Stokes" }
Explanation:
Here we have created a class student and inside that class, we have declared a __construct() function, which is executed when the object is created. The constructor receives arguments that are later provided when creating the object with the help of new keyword. In the first var_dump() expression we are printing the object, but in the second case, we are converting the object into an array with help of json_decode and json_encode technique.
Method 2:
Converting an object to an array with typecasting technique:
Typecasting is the approach to utilize one data type variable into the different data type and it is simply the exact transformation of a datatype.
<?php class bag { public function __construct( $item1, $item2, $item3){ $this->item1 = $item1; $this->item2 =$item2; $this->item3 = $item3; } } $myBag = new bag("Books", "Ball", "Pens"); echo "Before conversion :".'</br>'; var_dump($myBag); $myBagArray = (array)$myBag; echo "After conversion :".'</br>'; var_dump($myBagArray); ?>
Output:
Before conversion : object(bag)#1 (3) { ["item1"]=> string(5) "Books" ["item2"]=> string(4) "Ball" ["item3"]=> string(4) "Pens" } After conversion: array(3) { ["item1"]=> string(5) "Books" ["item2"]=> string(4) "Ball" ["item3"]=> string(4) "Pens" }
Explanation:
Here we have created a class "bag" and inside that class, we have declared a __construct() function, which is executed when the object is created. The constructor receives arguments that are later provided when creating the object with the help of the new keyword. In the first var_dump() expression, we are simply printing the object, but in the second case, we are type-hinting the object into an array with help of type-hinting procedure.
- Related Articles
- Convert an object to associative array in PHP
- How to convert an array to SimpleXML in PHP?
- How to convert an object array to an integer array in Java?
- How to convert an object into an array in JavaScript?
- How to convert an object to byte array in java?
- In PHP, how can I add an object element to an array?
- How to convert byte array to an object stream in C#?
- Convert JS array into an object - JavaScript
- How to convert PHP array to JavaScript array?
- Convert array of objects to an object of arrays in JavaScript
- Convert array of object to array of array in JavaScript
- How to convert Multidimensional PHP array to JavaScript array?
- How to convert array to string PHP?
- how to convert Object array to String array in java
- How can I convert the arguments object to an array in JavaScript?
