How to convert an array into object using stdClass() in PHP? Last Updated : 02 Sep, 2021 Comments Improve Suggest changes Like Article Like Report 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 ) Comment More infoAdvertise with us Next Article How to convert an array into object using stdClass() in PHP? P PranchalKatiyar Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to Convert Integer array to String array using PHP? Given is an integer Array, the task is to convert the Integer array to a String array and print the output.Example:Input: $integerArray = [10, 20, 30, 40, 50];Output: ["10", "20", "30", "40", "50"]These are the following approaches:Table of ContentUsing the array_map() and strval() functionUsing a l 4 min read How to Convert a String to JSON Object in PHP ? Given a String, the task is to convert the given string into a JSON object in PHP. JSON (JavaScript Object Notation) is a widely used data interchange format. PHP provides convenient functions to work with JSON data. Table of ContentUsing json_decode() to Convert to an ArrayHandling Errors with json 3 min read How to convert XML file into array in PHP? Given an XML document and the task is to convert an XML file into PHP array. To convert the XML document into PHP array, some PHP functions are used which are listed below: file_get_contents() function: The file_get_contents() function is used to read a file as string. This function uses memory mapp 2 min read Convert a String into an Array of Characters in PHP Given a string, the task is to convert the string into an array of characters using PHP. Examples:Input: str = "GFG" Output: Array( [0] => G [1] => f [2] => G)Input: str = "Hello Geeks"Output: Array( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => G [7] => 4 min read How to Convert Array to String in PHP? In PHP, Arrays are used to store multiple values in a single variable. However, sometimes we need to convert these arrays into strings:To display array data in a readable format.To join array items into a single sentence or list.To use the array data where only strings are accepted.To prepare data f 2 min read How to Convert Query String to an Array in PHP? In this article, we will see how to convert a query string to an array in PHP. The Query String is the part of the URL that starts after the question mark(?).Examples:Input: str = "company=GeeksforGeeks&address=Noida&mobile=9876543210"Output: Array ( [company] => GeeksforGeeks [ad 4 min read Convert an object to associative array in PHP An object is an instance of a class. It is simply a specimen of a class and has memory allocated. The array is the data structure that stores one or more similar types of values in a single name but an associative array is different from a simple PHP array. An array that contains a string index is c 3 min read How to Push Both Key and Value into PHP Array ? Given an array, the task is to push a new key and value pair into the array. There are some methods to push value and key into the PHP array, these are:Table of ContentUsing Square Bracket [] SyntaxUsing array_merge() FunctionUsing += OperatorUsing the array_combine FunctionUsing array_replace()Usin 4 min read How to Pass an Array into a Function in PHP ? This article will show you how to pass an array to function in PHP. When working with arrays, it is essential to understand how to pass them into functions for effective code organization and re-usability. This article explores various approaches to pass arrays into PHP functions, covering different 2 min read How to create an array for JSON using PHP? In this article, we will see how to create an array for the JSON in PHP, & will see its implementation through examples. Array: Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creatin 3 min read Like