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 // 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); ?> |
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 // Function to convert array into // stdClass object function ToObject($Array) { // Clreate 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); ?> |
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
)
Recommended Posts:
- Convert an object to associative array in PHP
- Convert an Array to an Object in JavaScript
- What is stdClass in PHP?
- How to convert a plain object into ES6 Map using JavaScript ?
- How to convert an object to string using JavaScript?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- Array.BinarySearch(Array, Object) Method with examples in C#
- How to add an object to an array in JavaScript ?
- How to Convert Array to Set in JavaScript?
- How to convert array to SimpleXML in PHP
- How to convert XML file into array in PHP?
- How to convert Set to Array in JavaScript?
- How to check object is an array in JavaScript?
- How to push an array into the object in JavaScript ?
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



