Possible Duplicate:
convert object to array
Let's say I have an array like this: (Notice that some of the methods/objects may be protected, so they have to be accessed in their own class)
array(
0=> objectname{
[method1:protected]=> array(
["key1"] => object2{
[method2]=> array(
0 => "blah"
)
}
)
}
1=> objectname{
[method1:protected]=> array(
["key1"] => object2{
[method2]=> array(
0 => "blah"
)
}
)
}
)
And I wanted to convert all of that into an Array. I would usually use this:
protected function _object_to_array($obj){
if(is_object($obj)) $obj = (array) $obj;
if(is_array($obj)) {
$new = array();
foreach($obj as $key => $val) {
$new[$key] = self::_object_to_array($val);
}
}else{
$new = $obj;
}
return $new;
}
The problem is that this does not preserve the object names. I would like the object names to become an extra key that bumps the array up a dimension. For example, replacing the 0 for objectname could work, but better yet to create something like this:
array(
0=> array(
objectname=> array(
...blah blah
)
)
)