2012-07-24 135 views
0

我想解析xml到數組中使用objectsIntoArray()使用PHP,我得到錯誤Call to undefined function objectsIntoArray()調用未定義的函數objectsIntoArray()-PHP

請人幫助

+2

您還沒有定義'objectsIntoArray()'。我相信你想要的是[此評論](http://www.php.net/manual/en/book.simplexml.php#97555)。 – 2012-07-24 06:54:08

+0

謝謝您Alvin Wong – phpwomen 2012-07-24 08:43:20

回答

2

SimpleXML Extension缺失,未安裝,或者您有錯誤的PHP版本或SimpleXML版本。

  1. 檢查phpinfo()和檢查http://www.php.net/manual/en/simplexml.installation.php
  2. 郵編

所以objectsIntoArray()它只是一個手工製作的功能。它不是內置在PHP中,所以你必須手動包含它。

<?php 

    function objectsIntoArray($arrObjData, $arrSkipIndices = array()){ 
     $arrData = array(); 
     if (is_object($arrObjData)) 
      $arrObjData = get_object_vars($arrObjData); 

     if (is_array($arrObjData)) 
      foreach ($arrObjData as $index => $value) { 
       if(is_object($value) || is_array($value)) 
        $value = objectsIntoArray($value, $arrSkipIndices); 

       if(in_array($index, $arrSkipIndices)) 
        continue; 

       $arrData[$index] = $value; 
      } 

     return $arrData; 
    } 

    $xmlString = " 
     <html> 
      <head> 
       <link blabla='whargarbl' /> 
      </head> 
     </html> 
    "; 
    $xmlObject = simplexml_load_string($xmlString); 
    $arrayXml = objectsIntoArray($xmlObject); 
    die(var_dump($arrayXml)); 
?> 
+0

在php.iniRevision中啓用Simplexml支持 - \t $修訂版:1.151.2.22.2.35.2.32 $和PHP版本是5.3.0.Please通知 – phpwomen 2012-07-24 07:00:57

+0

$ xmlString = file_get_contents($ fileName); $ xmlObject = simplexml_load_string($ xmlString); $ arrayXml = objectsIntoArray($ xmlObject); – phpwomen 2012-07-24 07:04:16

+0

這是一個對象成員。使用'$ arrayXml = $ xmlObject-> objectsIntoArray($ xmlObject); ' – 2012-07-24 07:07:10

1

objectsIntoArray()是不是一個標準的內置函數在PHP,所以如果你還沒有自己寫它,然後它會確實是不確定的。

如果您已經編寫了它,那麼也許您沒有包含包含該函數的相應文件。

我注意到有人在SimpleXML類的PHP手冊頁的註釋中發佈了一個具有相同名稱的函數 - 請參閱http://php.net/manual/en/book.simplexml.php並搜索objectsIntoArray。如果這是你正在尋找的功能,你可以直接從這裏複製+粘貼並使用它。

+0

感謝它的工作 – phpwomen 2012-07-24 08:41:59

相關問題