Back to Top

How to convert Object to array in PHP

how-to-convert-object-to-array-in-php

In this post, I am going to explain about to create an array from the object in PHP.Sometimes you get data in object form and you want to cast the object to the array.The biggest change from PHP 4 to PHP 5 is the new object Oriented Program.

PHP is quite simple and easy to use .An object is defined by the class of which it is an instance of the programmer-defined class.To access the member variable, an object is to follow a variable containing the object with -> and then the name of the member.when you assign an object to a variable to a function the object is actually copied bit-for-bit into the variable or function scope.

Here,I am going to explain to convert object form data to an array. Using the following function you can cast the object form data.

Many developers want to convert the object into an array for easy looping over a foreach somewhere in their code.You can attempt this in many ways. Here I just saw the one way which recursively converts your object into the array.

There are a few possible ways to convert the object into an array and each of them has its strengths and weaknesses. You can also use the following way.Using this way you can casting object with an array.

The problem with this is, it doesn’t convert multi-dimensional objects well. That’s why I suggest using above custom function which converts any object to an array.

That’s it. You can add this function and use it when require. If you have any comments or suggestion about this post, I would really appreciate it.

Share:
Share on Facebook1

Share on LinkedIn0Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

Leave a Reply

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

Most Popular Posts

How to use Micro Data in HTML5

Posted on 1 year ago

Bhumi

How to add more Social Media icons to Divi?

Posted on 12 months ago

Bhumi

Angular JS MVC Architecture with example

Posted on 2 years ago

Bhumi

Introduction to the Instagram API

Updated 4 weeks ago

Bhumi

How to Use the HTML5 Full-Screen API

Updated 1 month ago

Bhumi

What are Lambdas and Closures in PHP?

Updated 1 month ago

Bhumi

MySQL REGEXP Operator

Updated 1 month ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 2 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 3 years ago

Bhumi