function array2object($array) { if (is_array($array)) { $obj = new StdClass(); foreach ($array as $key => $val){ $obj->$key = $val; } } else { $obj = $array; } return $obj; } function object2array($object) { if (is_object($object)) { foreach ($object as $key ...
$person = new stdClass(); $person->firstName = "Taylor"; $person->age = 32; //Convert Single-Dimention Object to array $personArray = (array) $person; //Convert Multi-Dimentional Object to Array $personArray = objectToArray($person); function objectToArray ($object)...
// Create new stdClass Object$init=newstdClass;//Add some test data$init->foo = "Test data";$init->bar =newstdClass;$init->bar->baaz = "Testing";$init->bar->fooz =newstdClass;$init->bar->fooz->baz = "Testing again";$init->foox = "Just test";//Convert array to object a...
Converting anarray/stdClass->array The manual specifies this second parameter ofjson_decodeas: assoc WhenTRUE, returned objects will be converted into associative arrays. Hence it will convert the entire thing into an array: $array= json_decode(json_encode($booking),true); Source page:http://...
$init->bar = new stdClass; $init->bar->baaz = "Testing"; $init->bar->fooz = new stdClass; $init->bar->fooz->baz = "Testing again"; $init->foox = "Just test"; // Convert array to object and then object back to array ...
}// Useage:// Create new stdClass Object$init=newstdClass;// Add some test data$init->foo ="Test data";$init->bar =newstdClass;$init->bar->baaz ="Testing";$init->bar->fooz =newstdClass;$init->bar->fooz->baz ="Testing again";$init->foox ="Just test";// Convert array t...
Here’s an example below that converts a multi-dimensional array to an object. This is accomplished through recursion.<?php function arrayToObject($array) { if (!is_array($array)) { return $array; } $object = new stdClass(); if (is_array($array) && count($array) > 0) { foreac...
//Convert array into an object $object = json_decode(json_encode($carArray)); //Print array as an object using var_dump($object); ?> </> Copy Code Output – object(stdClass)#1 (1) { [“cars”] => array (3) { [0] => ...
array和object可以互转。如果其它任何类型的值被转换成对象,将会创建一个内置类stdClass的实例。 在我们写PHP扩展的时候,PHP内核提供了一组函数用于类型转换: void convert_to_long(zval* pzval) void convert_to_double(zval* pzval) void convert_to_long_base(zval* pzval, int base) ...
Ex 4 -Convert object to an array in PHP Here is an example of how to usejson_decode()to convert an object to an array in PHP: // Create a sample object $myObject = new stdClass(); $myObject->name = "John"; $myObject->age = 30; ...