PHP How to Convert Array to Object Easily
In PHP, you can convert an array to an object by casting it with
(object)$array or by using json_decode(json_encode($array)) to get a standard class object.Examples
Input['name' => 'Alice', 'age' => 25]
Outputobject(stdClass)#1 (2) { ["name"]=> string(5) "Alice" ["age"]=> int(25) }
Input[1, 2, 3]
Outputobject(stdClass)#1 (3) { ["0"]=> int(1) ["1"]=> int(2) ["2"]=> int(3) }
Input[]
Outputobject(stdClass)#1 (0) { }
How to Think About It
To convert an array to an object in PHP, think of the array keys as property names and the values as property values. Casting the array to an object creates a simple object where keys become properties. Alternatively, encoding the array to JSON and decoding it back as an object creates a standard class object with the same properties.
Algorithm
1
Get the input array.2
Cast the array to an object using (object) operator or encode to JSON and decode as object.3
Return the resulting object.Code
php
<?php $array = ['name' => 'Alice', 'age' => 25]; $obj = (object)$array; print_r($obj); // Alternative method $obj2 = json_decode(json_encode($array)); print_r($obj2); ?>
Output
stdClass Object
(
[name] => Alice
[age] => 25
)
stdClass Object
(
[name] => Alice
[age] => 25
)
Dry Run
Let's trace converting ['name' => 'Alice', 'age' => 25] to an object using (object) cast.
1
Start with array
array = ['name' => 'Alice', 'age' => 25]
2
Cast array to object
object = (object) array
3
Resulting object properties
object->name = 'Alice', object->age = 25
| Step | Action | Value |
|---|---|---|
| 1 | Input array | ['name' => 'Alice', 'age' => 25] |
| 2 | Cast to object | (object) array |
| 3 | Object properties | name: 'Alice', age: 25 |
Why This Works
Step 1: Casting array to object
Using (object)$array converts the array keys into object properties directly.
Step 2: Using JSON encode/decode
Encoding the array to JSON and decoding it back as an object creates a stdClass object with the same properties.
Alternative Approaches
Using (object) cast
php
<?php $array = ['a' => 1, 'b' => 2]; $obj = (object)$array; print_r($obj); ?>
Simple and fast, but creates a generic object without nested object conversion.
Using json_encode and json_decode
php
<?php $array = ['a' => 1, 'b' => 2]; $obj = json_decode(json_encode($array)); print_r($obj); ?>
Converts nested arrays to nested objects, but slower due to encoding and decoding.
Manual object creation
php
<?php $array = ['a' => 1, 'b' => 2]; $obj = new stdClass(); foreach ($array as $key => $value) { $obj->$key = $value; } print_r($obj); ?>
More control over conversion, useful for custom processing.
Complexity: O(n) time, O(n) space
Time Complexity
Conversion requires visiting each element once, so time grows linearly with array size.
Space Complexity
A new object is created with properties matching array keys, so space grows linearly.
Which Approach is Fastest?
Casting with (object) is fastest for shallow arrays; JSON methods handle nested arrays but are slower.
| Approach | Time | Space | Best For |
|---|---|---|---|
| (object) cast | O(n) | O(n) | Simple, shallow arrays |
| json_encode/json_decode | O(n) | O(n) | Nested arrays to objects |
| Manual foreach | O(n) | O(n) | Custom conversion logic |
Use
(object)$array for quick conversion when nested arrays are not involved.Trying to convert arrays with nested arrays using only (object) cast, which does not convert nested arrays to objects.