Answers for "convert array to object in php example"

PHP
0

convert array to object in php example

<?php
function toObject($arr) {
    if (is_array($arr)) {
        // Return object 
        return (object) array_map('toObject', $arr);
    }
     return false;
}
$empInfo = array(
    'name'=>'John',
    'address'=>'Houston',
    'employment' => array 
        (
           'id' => '1',
           'address' => 'Los Angeles'
        )
);
print_r(toObject($empInfo));

/*_____output_______*/
// OUTPUT

/*stdClass Object 
( 
   [name] => John 
   [address] => Houston 
   [employment] => stdClass Object 
      ( 
         [id] => 1 
         [address] => Los Angeles 
      ) 
)*/
Posted by: Guest on October-28-2021

Code answers related to "convert array to object in php example"

Browse Popular Code Answers by Language