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.

Updated on: 30-Jul-2019

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements