Php XML to Array功能

时间:2013-04-04 16:30:09

标签: php xml xml-parsing simplexml

嗨大家我试图编写一个函数,将xml转换为带有simplexml的数组。我知道有些功能已经写好但我需要自己编写。这个xml只是一个例子,可能是不同的格式,所以我需要这个函数与任何类型的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>myemail@gmail.com</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
            (
            )

    )

这很棒,但现在我需要不断循环并添加所有标签,直到没有更多。

1 个答案:

答案 0 :(得分:0)

这是我有时使用的功能:

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

        return $out;
}