Thursday, October 9, 2025
HomeLanguagesHow to convert an array into object using stdClass() in PHP?

How to convert an array into object using stdClass() in PHP?

To convert an array into the object, stdClass() is used. The stdClass() is an empty class, which is used to cast other types to object. If an object is converted to object, its not modified. But, if object type is converted/type-casted an instance of stdClass is created, if it is not NULL. If it is NULL, the new instance will be empty.

Example 1: It converts an array into object using stdClass. 

PHP




<?php
 
// Function to convert array into
// stdClass object
function ToObject($Array) {
     
    // Create new stdClass object
    $object = new stdClass();
     
    // Use loop to convert array into
    // stdClass object
    foreach ($Array as $key => $value) {
        if (is_array($value)) {
            $value = ToObject($value);
        }
        $object->$key = $value;
    }
    return $object;
}
 
// Declare an array and initialize it
$Original = array (
    '1' => array(
        'sNo' => '1',
        'Age' => '20',
        'name' => 'A'
    ),
    '2' => array(
        'sNo' => '2',
        'Age' => '21',
        'name' => 'B'
    ),
    '3' => array(
        'sNo' => '3',
        'Age' => '22',
        'name' => 'C'
    ),
    '4' => array(
        'sNo' => '4',
        'Age' => '23',
        'name' => 'D'
    ),
    '5' => array(
        'sNo' => '5',
        'Age' => '24',
        'name' => 'E'
    )
);
 
// Display the original array
print_r($Original);
 
// Function call
$convertedObj = ToObject($Original);
 
// Display the stdClass object
print_r($convertedObj);
 
?>


Output: 

Array
(
    [1] => Array
        (
            [sNo] => 1
            [Age] => 20
            [name] => A
        )

    [2] => Array
        (
            [sNo] => 2
            [Age] => 21
            [name] => B
        )

    [3] => Array
        (
            [sNo] => 3
            [Age] => 22
            [name] => C
        )

    [4] => Array
        (
            [sNo] => 4
            [Age] => 23
            [name] => D
        )

    [5] => Array
        (
            [sNo] => 5
            [Age] => 24
            [name] => E
        )

)
stdClass Object
(
    [1] => stdClass Object
        (
            [sNo] => 1
            [Age] => 20
            [name] => A
        )

    [2] => stdClass Object
        (
            [sNo] => 2
            [Age] => 21
            [name] => B
        )

    [3] => stdClass Object
        (
            [sNo] => 3
            [Age] => 22
            [name] => C
        )

    [4] => stdClass Object
        (
            [sNo] => 4
            [Age] => 23
            [name] => D
        )

    [5] => stdClass Object
        (
            [sNo] => 5
            [Age] => 24
            [name] => E
        )

)

 

Example 2: It converts an array into object using stdClass. 

PHP




<?php
 
// Function to convert array into
// stdClass object
function ToObject($Array) {
     
    // Create new stdClass object
    $object = new stdClass();
     
    // Use loop to convert array into object
    foreach ($Array as $key => $value) {
        if (is_array($value)) {
            $value = ToObject($value);
        }
        $object->$key = $value;
    }
    return $object;
}
 
// Declare an array
$Original = array(1, 2, 3, 4, 5, 6);
 
// Display the array element
print_r($Original);
 
// Function call to convert object
$convertedObj = ToObject($Original);
 
// Display the stdClass object
print_r($convertedObj);
 
?>


Output: 

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
stdClass Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

 

RELATED ARTICLES

Most Popular

Dominic
32346 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6790 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS