Change language

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

| |
Example 1:It converts an array to an object using stdClass.  
// Function to convert the array to
// stdClass object function ToObject ( $Array ) { // Create a new stdClass object $object = new stdClass(); // Using a loop to convert an array to // 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’ ) ) ;  
// Show original array print_r ( $Original );  
// function call $convertedObj = ToObject ( $Original );  
// Show stdClass object print_r ( $convertedObj );  
?>
Exit:
 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 to an object using stdClass.  
// Function for transforming the array in
// stdClass object function ToObject ( $Array ) { // Create a new stdClass object $object = new stdClass(); // Using a loop to convert an array to an object foreach ( $Array as $key = > $value ) { if ( is_array ( $value )) { $value = ToObject ( $value ); } $object -> $key = $value ; } return $object ; }  
// Declare the array $Original = array (1, 2, 3, 4, 5, 6);  
// Show array element print_r ( $Original );  
// call the function to transform the object $convertedObj = ToObject ( $Original );  
// Show stdClass object print_r ( $convertedObj );  
?>
Exit:
 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) 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method