2013-04-04 33 views
0

嗨!我試圖編寫一個函數,它將使用simplexml將xml轉換爲數組。我知道那裏有已經寫好的函數,但我需要自己寫這個。這個xml只是一個例子,可能是不同的格式,所以我需要這個函數與任何類型的xml結構一起使用,基本上都是通過所有的標籤並遍歷所有的標籤並且遍歷所有的標籤並創建數組。Php XML到數組函數

繼承人我的XML是什麼樣子

<approval id='x34445454'> 
     <p_data> 
    <p_wide>3.5</p_wide> 
    <p_hieight>2</p_hieight> 
     </p_data> 
    <bars></bars> 
    <timer> 
    <loc_time> 
     <year>2013</year> 
     <month>4</month> 
     <day>2</day> 
    </loc_time> 
    <start> 
     <year>2013</year> 
     <month>04</month> 
     <day>02</day> 
    </start> 
    <end> 
     <year>2013</year> 
     <month>04</month> 
     <day>09</day> 
    </end> 
</timer> 
<customer_data> 
    <custid>8789</custid> 
    <email>[email protected]</email> 
    <description>Some item description</description> 
    <optout>false</optout> 
</customer_data> 
<large>2</large> 
<job name='ord1'> 
    <guides></guides> 

    <preview> 
     <type>mytpe</type> 
     <info_message></info_message> 
     <label>both</label> 
     <index>100</index> 
     <imagebox> 
      <data>703.448275862069</data> 
      <height>11.068965517241</height> 
      <shift>0</shift> 
     </imagebox> 
     <OK_Screen></OK_Screen> 
    </preview_file> 
    <preview_file> 
     <type>mytpe3</type> 
     <info_message></info_message> 
     <label>sides</label> 
     <index>100</index> 
     <imagebox> 
      <data>111.448275862069</data> 
      <height>11.554</height> 
      <shift>0</shift> 
     </imagebox> 
     <OK_Screen></OK_Screen> 
    </preview> 
</job> 

<job name='ord1'> 
    <guides></guides> 

    <preview> 
     <type>mytpe</type> 
     <info_message></info_message> 
     <label>both</label> 
     <index>100</index> 
     <imagebox> 
      <data>703.448275862069</data> 
      <height>11.068965517241</height> 
      <shift>0</shift> 
     </imagebox> 
     <OK_Screen></OK_Screen> 
    </preview_file> 
    <preview_file> 
     <type>mytpe3</type> 
     <info_message></info_message> 
     <label>sides</label> 
     <index>100</index> 
     <imagebox> 
      <data>111.448275862069</data> 
      <height>11.554</height> 
      <shift>0</shift> 
     </imagebox> 
     <OK_Screen></OK_Screen> 
    </preview> 
</job> 

到目前爲止IV寫了這個功能

function xmltest($xml) { 
     $arr = array(); 

foreach($xml as $element) { 
    $arr[$element->getName()]=array(); 
    foreach($element->attributes() as $key=>$value) { 
     $arr[$element->getName()][$key]=(string)$value; 
    } 

    foreach($element as $key=>$value) { 
     $arr[$element->getName()][$key]=array(); 

     if(!$value->children() && (!empty($value))) { 
      $arr[$element->getName()][$key]=(string)$value; 
     } 


    } 

    } 

    return $arr; 


} 

這是返回

Array 
    (
[approval] => Array 
    (
     [id] => x34445454 
     [p_data] => Array 
      (
      ) 

     [bars] => Array 
      (
      ) 

     [timer] => Array 
      (
      ) 

     [customer_data] => Array 
      (
      ) 

     [large] => 2 
     [job] => Array 
      (
      ) 

    ) 

這是偉大的,但現在我需要不斷循環,並添加所有的標籤,直到沒有更多。

回答

0

這裏是我有時使用的功能:

public function xmlToArray($xmlObject, $out = array()) 
{ 
     foreach ((array) $xmlObject as $index => $node) 
      $out[$index] = (is_object ($node)) ? $this->xmlToArray($node) : $node; 

     return $out; 
} 
+0

這似乎沒有工作。沒有錯誤輸出或奇怪 – Yeak 2013-04-04 18:03:16

+0

它給你什麼? – liz 2013-04-04 19:39:54

+0

它沒有返回任何東西。 :( – Yeak 2013-04-04 20:05:35