/**
* PHP 物件、陣列間的轉換
*
* @author flyer0126
* @since **/
// 1. 利用(array)和(object),簡單處理
$objTemp = (object)array();
$objTemp = (object)array();$- 1 ;
$objTemp->b = 2;
$objTemp->c = 3;
$arrTemp = (array)$objTemp;
print_r($objTemp);
/**
stdClass 物件
(
[a] => 1
[b] => 2
[c] => 3333333353() ] => ; 1
[b] => 2
[c] => 3
)
**/
// PS:簡單的(array)和(object)只能處理單層的數據,對於多層的陣列和物件轉換則無能為力。// 2. 多維數組與物件間的轉換處理
/**
* 將物件轉換為多維陣列
function objectToArray($m
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
/* * Return array converted to object * Using __FUNCTION__ (Magic constant) * for recursive call */* for recursive call */
// Return array
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return (objursive call
*/
return (objursive) array_map( */
) / / Return object return $d; }}// Useage:
>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$ arrayToObject($array);
// Print objects and array
print_r($init);
print_r($array); #&*/