複製程式碼 程式碼如下:
function arrayToObject($e){
if( gettype($e)!='array ' ) return;
foreach($e as $k=>$v){
if( gettype($v)=='array' || getType($v)=='object' )
$e[$k]=(object)arrayToObject($v);
}
return (object)$e;
}
function objectToArray($e){
$ee>function objectToArray($e){
$ee =(array)$e;
foreach($e as $k=>$v){
if( gettype($v)=='resource' ) return;
if( gettype($v )=='object' || gettype($v)=='array' )
$e[$k]=(array)objectToArray($v);
}
return $e;
}
// Gets the properties of the given object // with get_object_vars function $d = get_object_vars($d); } if (is_array($d)) {
/** Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
return $d;
}
}
Tounction arrayarray. ($d) {
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return (object) array_map(__FUNCTION__, $d);
}
else {
// Return object
return $d;
}
}
}
// Useage:
// Create new stdClass Object
$init = new stdClass;
// Add some test data
$init->foo = "Test data";
$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
$array = objectToArray($in)ToArray($in) ;
$object = arrayToObject($array);
// Print objects and array
print_r($init);
echo "n";
print_r($array);
echo "n";
print_r($array); echo "n";
print_r($object);
?>
以上就介紹了火炬之光有多少層php簡單物件與陣列的轉換函數程式碼php多層陣列和物件的轉換,包括了火炬之光有多少層方面的內容,希望對PHP教程有興趣的朋友有所幫助。