Convert object to array using php

Home > Full Staông xã Development > How to Convert Object khổng lồ Array in PHPhường

Bạn đang xem: Convert object to array using php

PHP is one of the most popular general-purpose scripting languages used widely for web development. It is one of the faschạy thử and flexible programming languages. Working with PHPhường 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 lớn convert an object to array in PHPhường. .

Object-Oriented Programming (OOP) in PHPSome OOPhường. ConceptsClassObjectExample Defining a Class và its ObjectsArrayDefining an ArrayObject to Array PHP1. Typecasting Object lớn Array PHP2. Using the JSON Decode & Encode MethodConclusion

Xem thêm: Tiết Lộ Bí Quyết Nấu Ăn Ngon Của Đầu Bếp Siêu Hạng, Bí Quyết Nấu Ăn Ngon Của Đầu Bếp Võ Quốc

Object-Oriented Programming (OOP) in PHPhường

One of the key aspects of PHP is object-oriented programming where the data is treated as an object, & software is implemented on it. This is one of the simplified approaches of advanced PHPhường. Object-oriented programming is achievable with PHPhường where objects have sầu 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..


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


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

// Creating three objects of Jobs

$software = new Jobs;

$pharmaceutical = new Jobs;

$finance = new Jobs;


An array, in PHPhường., 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()’.


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

echo count($numbers);


Read: 15 Exciting PHPhường Project Ideas & Topics For Beginners

Object lớn Array PHP..

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

1. By typecasting  object to array PHP

2. Using the JSON Decode & Encode Method

Let’s have sầu a look at both in detail:

1. Typecasting Object lớn Array PHP

Typecasting is a method where one data type variable is utilized into lớn a different data type, và it is simply the exact conversion of a data type.

In PHP.., an object can be converted khổng lồ an array with the typecasting rules of PHPhường.


$myArray = (array) $myObj;


   class cửa hàng


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

   emang đến “Before conversion :”."


   $myShopArray = (array)$myShop;

   emang đến “After conversion :”."




  Before conversion:

object(shop)#1 (3)

After conversion:


 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 và 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 và converts it inlớn a PHP.. array.


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


   class employee


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

   eđến “Before conversion:”."


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

   emang lại “After conversion:”."




Before conversion:

object(student)#1 (2)

After conversion:


Explanation of the program:

In the program above sầu, 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 keywords. In the first var_dump() expression, the object is printed và in the second, the object is converted inlớn an array using json_decode & json_encode technique.

Also Read: 15 Interesting PHP.. Projects on Github For Beginners


In this article, we have sầu introduced one of the most prominent topics of PHPhường. 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 lớn the advanced màn chơi. Hope this article highlighting the methods for converting an  object lớn array in PHPhường  will prove out to be a reference point for you. brings programming with PHPhường and a lot more with’s  PG Diploma in Software Development Specialisation in Full Staông chồng Development . A program lớn make you emerge as a full staông chồng developer & learning to lớn build some of the awesome applications. It is an extensive 12-months program that includes working on live projects & assignments & also training 15 programming languages & tools. Along with it, it has all-time career support with mochồng interviews and job assistance.