2013-04-04 140 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