2012-04-05 35 views
0

您好我有與它的目的,當我投它作爲一個陣列中,只有第一電平改變到一個數組一個std類對象。是否有一個爲什麼要將子對象作爲數組轉換?角色陣列超出第一級

的var_dump($ heyo);

object(stdClass)#167 (27) { 
    ["uid"]=> object(stdClass)#166 (1) { 
     ["1"]=> int(15) 

var_dump((array)($ heyo));

array(27) { 
    ["uid"]=> object(stdClass)#166 (1) { 
     ["1"]=> int(15) 
+0

你能告訴我們一些示例代碼? – Shoe 2012-04-05 17:08:22

+1

我喜歡一些更多的細節。你如何施展?對象是什麼樣的?它有不同的深度,還是固定的? – Hubro 2012-04-05 17:08:35

+0

@Codemonkey固定 – 2012-04-05 17:13:38

回答

1

我發現在if-not-true-then-false.com

function objectToArray($d) { 
    if (is_object($d)) { 
     // 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; 
    } 
} 

這個功能它會遞歸轉換你stdClass的對象數組