How to Convert Object into Array in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hi Developer,

In this short tutorial, we will share the quick and straightforward way to php convert object to array. This post will give you a simple example of convert stdclass object to array in php. If you have a question about php object to array convert then I will give a simple example with a solution. Here you will learn php convert object to array example

Whenever you need to convert object to array in your php project then you can do that using (array). It is a pretty simple example to convert php object into array that way you can use array. So, let's see the following example.

Example:

<?php

class MyClass {

}

$myObject = new MyClass;

$myObject->id = 100;

$myObject->title = "posts";

$myObject->description = "this is posts";

$newArray = (array) $myObject;

print_r($newArray);

?>

Output:

Array

(

[id] => 100

[title] => posts

[description] => this is posts

)

I hope it can help you...

Tags :
Shares