1

嘿伙计们,我有一个方法应该回调并递归地将一个Arrayobject转换为关联数组,不幸的是我遇到了一个致命错误

PHP Fatal error:  Call to undefined method ArrayObject::toArray()

这是我的方法

/**
 * Take an ArrayObject and recursively turn it into an array
 * 
 * @param bool $recursion
 * 
 * @return array
 */
public function toArray($recursion = false) 
{
    // just in case the object might be multidimensional
    if (true === $this->object) return $this->object->getArrayCopy();

    return array_map( function($item)
    {
        return is_object($item) ? $item->toArray(true) : $item;

    }, $this->object->getArrayCopy() );
}

这是一个示例ArrayObject

ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [profile] => ArrayObject Object
                (
                    [storage:ArrayObject:private] => Array
                        (
                            [list] => ArrayObject Object
                                (
                                    [storage:ArrayObject:private] => Array
                                        (
                                            [location] => 
                                            [network_name] => 
                                            [interests] => 
                                            [last_name] => 
                                            [url] => 
                                            [significant_other] => 
                                            [network_domains] => 
                                            [contact] => ArrayObject Object
                                                (
                                                    [storage:ArrayObject:private] => Array
                                                        (
                                                            [im] => ArrayObject Object
                                                                (
                                                                    [storage:ArrayObject:private] => Array
                                                                        (
                                                                            [provider] => 
                                                                        )

                                                                )

                                                            [email_addresses] => 
                                                        )

                                                )

                                        )

                                )

                        )

                )

            [messages] => ArrayObject Object
                (
                    [storage:ArrayObject:private] => Array
                        (
                            [list] => Array
                                (
                                    [0] => foo
                                    [1] => bar
                                    [2] => baz
                                )

                        )

                )

            [groups] => ArrayObject Object
                (
                    [storage:ArrayObject:private] => Array
                        (
                        )

                )

            [users] => ArrayObject Object
                (
                    [storage:ArrayObject:private] => Array
                        (
                        )

                )

        )

)
4

1 回答 1

0

这可以正常工作,通过$this作为变量传递,我不需要担心与冲突$this->object或范围问题。

/**
 * Public wrapper for Protected getArray()
 * 
 * @return arrau
 */
public function toArray()
{
    return $this->getArray($this->object);
}

/**
 * Take an ArrayObject and turn it into an associative array
 * 
 * @param ArrayObject $obj
 * 
 * @return array
 */
protected function getArray($obj) 
{
    $array  = array(); // noisy $array does not exist
    $arrObj = is_object($obj) ? get_object_vars($obj) : $obj;
    foreach ($arrObj as $key => $val) {
            $val = (is_array($val) || is_object($val)) ? $this->getArray($val) : $val;
            $array[$key] = $val;
    }
    return $array;
}
于 2013-04-18T19:10:51.780 回答