2013-04-18 46 views
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 
         (
         ) 

       ) 

     ) 

) 
+0

你在哪裏以及如何調用函數toArray函數。 –

+0

在另一個類中,我實例化解析器,解析輸入,將該值賦值給'$ this-> object',然後運行'$ foo = $ bar-> toArray()'。實例化和利用公共方法調用的典型方法。 – ehime

+0

嘗試將函數的名稱替換爲toArray()更改爲其他名稱。第二件事是,作爲錯誤,你正在創建解析器的對象。您正在通過數組對象調用函數。重新檢查你的代碼。 –

回答

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; 
}