2012-04-05 49 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的对象数组