Answers for "php convert associative array to object"

PHP
9

php convert array to object

$object = (object) $array;
Posted by: Guest on March-17-2020
1

convert array to object php

$arrayResult = array_map(function($array){
    return (object)$array;
}, $yourOrinalArray);
Posted by: Guest on July-17-2020
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 "php convert associative array to object"

Browse Popular Code Answers by Language