1
嗨我是應該回調和遞歸把一個Arrayobject
成一個關聯數組的方法,不幸的是我發現了一個致命的錯誤PHP遞歸地翻動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
(
)
)
)
)
你在哪裏以及如何調用函數toArray函數。 –
在另一個類中,我實例化解析器,解析輸入,將該值賦值給'$ this-> object',然後運行'$ foo = $ bar-> toArray()'。實例化和利用公共方法調用的典型方法。 – ehime
嘗試將函數的名稱替換爲toArray()更改爲其他名稱。第二件事是,作爲錯誤,你正在創建解析器的對象。您正在通過數組對象調用函數。重新檢查你的代碼。 –