1

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
        )
    )
)
Community
  • 1
  • 1
CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98
  • Not a duplicate. I used that code to construct my first process. – CMCDragonkai Dec 03 '12 at 17:11
  • No, it's a duplicate. The key to doing this right is in the first accepted answer to that question: call `get_object_vars()` within the object you're wishing to convert to an array. That will cope correctly with protected and private member variables. Also, there's a difference between methods and member variables: it appears you're mixing the terms up. For a start, methods won't appear in the resulting array if you case their object to an array, whereas member variables will. This gist I created should demonstrate things: https://gist.github.com/4196621 – Keith Gaughan Dec 03 '12 at 17:38
  • The specific problem I had wasn't with getting object vars. It was getting the name of the object. Which I solved with this $obj_name = get_class($obj); – CMCDragonkai Dec 03 '12 at 22:09

1 Answers1

2

Figured it out.

However new problem, the protected methods endup as keys like [*formermethodturnedkey]. They don't seem to be accessible. How can one access keys like this?

protected function _object_to_array($obj){

    //we want to preserve the object name to the array
    //so we get the object name in case it is an object before we convert to an array (which we lose the object name)
    $obj_name = false;
    if(is_object($obj)){
        $obj_name = get_class($obj);
        $obj = (array) $obj;
    }

    //if obj is now an array, we do a recursion
    //if obj is not, just return the value
    if(is_array($obj)) {

        $new = array();

        //initiate the recursion
        foreach($obj as $key => $val) {
            //we don't want those * infront of our keys due to protected methods
            $new[$key] = self::_object_to_array($val);
        }

        //now if the obj_name exists, then the new array was previously an object
        //the new array that is produced at each stage should be prefixed with the object name
        //so we construct an array to contain the new array with the key being the object name
        if(!empty($obj_name)){
            $new = array(
                $obj_name => $new,
            );
        }

    }else{

        $new = $obj;

    }

    return $new;

}
CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98