How to Convert Object to Array in PHP [With Example]

PHP is one of the most popular general-purpose scripting languages used widely for web development. It is one of the fastest and flexible programming languages. Working with PHP is all about dealing with data types. There are several data types in PHP in which objects and arrays are the composite data types of PHP. This article is centred on how to convert an object to array in PHP.

Object-Oriented Programming (OOP) in PHP

One of the key aspects of PHP is object-oriented programming where the data is treated as an object, and software is implemented on it. This is one of the simplified approaches of advanced PHP. Object-oriented programming is achievable with PHP where objects have rules defined by a PHP program they are running in. These rules are called the classes.

Some OOP Concepts

Before getting into how objects are converted into arrays, let’s first learn about some important terms related to object-oriented programming in PHP.

Class

Classes are the data types defined by a programmer. It includes local function and local data. A class can serve as a template for making multiple instances of the same class of objects.

Object

An individual instance of the data structure is defined by a class. Many objects belonging to a class can be made after defining a class once. Objects are also called as instances.

Example Defining a Class and its Objects

class Jobs {

   // Members of class Jobs

}

// Creating three objects of Jobs

$software = new Jobs;

$pharmaceutical = new Jobs;

$finance = new Jobs;

Array

An array, in PHP, is a special kind of variable that holds more than one value at a time.

Defining an Array

In PHP, the array is defined with the array function ‘array()’.

Example:

<?php

$numbers = array(“One”, “Two”, “Three”);

echo count($numbers);

?>

Read: 15 Exciting PHP Project Ideas & Topics For Beginners

Object to Array PHP

There are mainly two methods by which an object is converted into an array in PHP:

1. By typecasting object to array PHP

2. Using the JSON Decode and Encode Method

Let’s have a look at both in detail:

1. Typecasting Object to Array PHP

Typecasting is a method where one data type variable is utilized into a different data type, and it is simply the exact conversion of a data type.

In PHP, an object can be converted to an array with the typecasting rules of PHP.

Syntax:

$myArray = (array) $myObj;

Program:

<?php

   class shop {

      public function __inventory( $product1, $product2, $product3){

         $this->product1 = $product1;

         $this->product2 =$product2;

         $this->product3 = $product3;

      }

   }

   $myShop= new shop(“Grocery”, “Cosmetic”, “Grain”);

   echo “Before conversion :”.'</br>’;

   var_dump($myShop);

   $myShopArray = (array)$myShop;

   echo “After conversion :”.'</br>’;

   var_dump($myShopArray);

?>

Output:

 Before conversion:

object(shop)#1 (3) { [“product1″]=> string(5) ” Grocery ” [“product2″]=> string(4) ” Cosmetic ” [“product3″]=> string(4) ” Grain ” }

After conversion:

array(3) { [“product1″]=> string(5) ” Grocery ” [“product2″]=> string(4) ” Cosmetic ” [“product3″]=> string(4) ” Grain ” }

 Explanation of the program:

In the above program, a class “shop” is created. In the ‘shop’ class, the function ‘inventory()’ is created. The function inventory() will be executed when an object is created.

The constructor will receive arguments provided when the object is created with a new keyword. In the first var_dump() expression, the object is printed. The second time, the object is type casted into an array using the type-casting procedure.

2. Using the JSON Decode and Encode Method

Object to array PHP is also done with the JSON decode and encode method. In this method, the json_encode() function returns a JSON encoded string for a given value. The json_decode() function accepts the JSON encoded string and converts it into a PHP array.

Syntax:

$myArray = json_decode(json_encode($object), true);

Program:

<?php

   class employee {

      public function __company($firstname, $lastname) {

         $this->firstname = $firstname;

         $this->lastname = $lastname;

      }

   }

   $myObj = new employee(“Carly”, “Jones”);

   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) ” Carly ” [“lastname”]=> string(6) ” Jones ” }

After conversion:

array(2) { [“firstname”]=> string(4) ” Carly ” [“lastname”]=> string(6) ” Jones ” }

Explanation of the program:

In the program above, a class with the name ‘employee’ is created. In that class, a function ‘company()’ is declared which will be executed during the creation of the object.

The constructor receives the arguments given when creating the object using a new keyword. In the first var_dump() expression, the object is printed and in the second, the object is converted into an array using json_decode and json_encode technique.

Also Read: 15 Interesting PHP Projects on Github For Beginners

Conclusion

In this article, we have introduced one of the most prominent topics of PHP. As a programmer, you will be dealing with every aspect of the language and have to work on some of the most complicated PHP concepts when moving to the advanced level. Hope this article highlighting the methods for converting an object to array in PHP will prove out to be a reference point for you.

upGrad brings programming with PHP and a lot more with upGrad’s PG Diploma in Software Development Specialisation in Full Stack Development. A program to make you emerge as a full stack developer and learning to build some of the awesome applications. It is an extensive 12-months program that includes working on live projects and assignments and also training 15 programming languages and tools. Along with it, it has all-time career support with mock interviews and job assistance.

Land on Your Dream Job

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK
Learn More

Leave a comment

Your email address will not be published. Required fields are marked *

×