0
0
PhpHow-ToBeginner · 2 min read

PHP How to Convert Object to Array Easily

In PHP, you can convert an object to an array by using (array)$object to cast it or get_object_vars($object) to get its properties as an array.
📋

Examples

Inputobject with properties {"name":"John", "age":30}
Output{"name":"John", "age":30}
Inputempty object {}
Output{}
Inputobject with nested object {"person":{"name":"Anna"}}
Output{"person":{"name":"Anna"}}
🧠

How to Think About It

To convert an object to an array in PHP, think about extracting the object's properties and their values into a list format. You can do this by casting the object to an array or by using a built-in function that returns the object's properties as an array. This helps when you want to work with array functions or need a simpler data structure.
📐

Algorithm

1
Get the object you want to convert.
2
Use a method to extract its properties as an array.
3
Return or use the resulting array.
💻

Code

php
<?php
$obj = new stdClass();
$obj->name = "John";
$obj->age = 30;

// Method 1: Casting
$array1 = (array)$obj;
print_r($array1);

// Method 2: get_object_vars
$array2 = get_object_vars($obj);
print_r($array2);
?>
Output
Array ( [name] => John [age] => 30 ) Array ( [name] => John [age] => 30 )
🔍

Dry Run

Let's trace converting an object with properties name='John' and age=30 to an array.

1

Create object

Object has properties: name='John', age=30

2

Cast object to array

Resulting array: ['name' => 'John', 'age' => 30]

3

Use get_object_vars

Returns array: ['name' => 'John', 'age' => 30]

StepActionResult
1Create object{name: 'John', age: 30}
2Cast to array['name' => 'John', 'age' => 30]
3get_object_vars['name' => 'John', 'age' => 30]
💡

Why This Works

Step 1: Casting object to array

Using (array)$object converts the object properties into an associative array with keys as property names.

Step 2: Using get_object_vars function

The get_object_vars() function returns an associative array of accessible properties of the object.

🔄

Alternative Approaches

json encode and decode
php
<?php
$obj = new stdClass();
$obj->name = "John";
$obj->age = 30;

$array = json_decode(json_encode($obj), true);
print_r($array);
?>
This method converts the object to JSON string and back to array, useful for nested objects but slower.

Complexity: O(n) time, O(n) space

Time Complexity

Converting an object to an array requires visiting each property once, so it takes linear time relative to the number of properties.

Space Complexity

A new array is created to hold the properties, so space usage grows linearly with the number of properties.

Which Approach is Fastest?

Casting with (array) is fastest and simplest; get_object_vars() is similar; JSON encode/decode is slower but handles nested objects better.

ApproachTimeSpaceBest For
(array) castO(n)O(n)Simple, shallow objects
get_object_vars()O(n)O(n)Accessible properties only
json encode/decodeO(n)O(n)Nested objects, deep conversion
💡
Use (array)$object for a quick and simple object to array conversion in PHP.
⚠️
Trying to use array functions directly on objects without converting them first.