Back to Top

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.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to add ColorDots in FullCalender?

Posted on 4 years ago


Control Flow Functions in MySQL

Posted on 3 years ago


Django Tutorial for beginners

Posted on 2 years ago