PHP:将XML反序列化为数组

时间:2017-07-27 23:53:52

标签: php arrays xml

一个月前我问过如何在c#中反序列化XML,现在我必须在PHP中执行相同的操作,但我无法使其正常工作,这是我的XML格式:

<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/api/" action="EXPORT">
<result>
    <rows>
        <row>
            <column name="Name1">Value1</column>
            <column name="Name2">Value2</column>
        </row>
        <row>
            <column name="Name1">Value1</column>
            <column name="Name2">Value2</column>
        </row>
    </rows>
</result>
</response>

这就是我要做的事情:

$row = new SimpleXMLElement($XML);

    $json = json_encode($row);
    $array = json_decode($json,TRUE);


    foreach($array["result"]["rows"]["row"] as $row){

           $array[$k] = [
                    'Name1' => $row["column"][0],
                    'Name2' => $row["column"][1]
                    ];

              $k++;
    }

这个问题是,如果使用foreach就像:

foreach($array as $arr){
            print_r($arr);
            echo"<br>";
}

给我这个结果:

  

数组([uri] =&gt; / api / [action] =&gt;导出)

     

数组([rows] =&gt;数组([row] =&gt;数组([0] =&gt;数组([column] =&gt;   数组([0] =&gt; Value1 [1] =&gt; Value2))[1] =&gt;数组([column] =&gt;   数组([0] =&gt; Value1 [1] =&gt; Value2)))))

     

数组([Name1] =&gt; Value1 [Name2] =&gt; Value2)

     

数组([Name1] =&gt; Value1 [Name2] =&gt; Value2)

我只想要打印的最后2个结果,因为这实际上是我需要的,我该如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

你几乎就在那里。这就是我想出来的

<?php

$XML='<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/api/" action="EXPORT">
<result>
    <rows>
        <row>
            <column name="Name1">Value1</column>
            <column name="Name2">Value2</column>
        </row>
        <row>
            <column name="Name1">Value1</column>
            <column name="Name2">Value2</column>
        </row>
    </rows>
</result>
</response>';

$row = new SimpleXMLElement($XML);

$json = json_encode($row);
$array = json_decode($json,TRUE);

foreach($array['result']['rows']['row'] as $key=>$value){
    echo '<pre>';
    print_r($value);
    echo '</pre>';
}

?>

Array
(
    [column] => Array
        (
            [0] => Value1
            [1] => Value2
        )

)

Array
(
    [column] => Array
        (
            [0] => Value1
            [1] => Value2
        )

)

您可以使用$ value [&#39;列&#39;] [&#39; 0&#39;]获取Value1

你也可以开始第二个foreach来循环它们

<?php

foreach($array['result']['rows']['row'] as $key=>$value){
    foreach($value['column'] as $k=>$v){
        echo "[Name".$k++."] = $v<br />";
        // or you can have it in an array like this
        $new_array[]["Name".$k++]=$v;
    }
}

?>

[Name1] = Value1
[Name2] = Value2
[Name1] = Value1
[Name2] = Value2

$ new_array看起来像

Array
(
    [0] => Array
        (
            [Name1] => Value1
        )

    [1] => Array
        (
            [Name2] => Value2
        )

    [2] => Array
        (
            [Name1] => Value1
        )

    [3] => Array
        (
            [Name2] => Value2
        )

)

您也可以通过更改字符串末尾的[]来将它们组合在一起,就像这样

$new_array["Name".$k++][]=$v;

结果将是:

Array
(
    [Name1] => Array
        (
            [0] => Value1
            [1] => Value1
        )

    [Name2] => Array
        (
            [0] => Value2
            [1] => Value2
        )

)

您可以使用第一个循环的$ key编号,而不是使用[],而是将它们分组不同

$new_array[$key]["Name".$k++]=$v;

将输出和数组这样

Array
(
    [0] => Array
        (
            [Name1] => Value1
            [Name2] => Value2
        )

    [1] => Array
        (
            [Name1] => Value1
            [Name2] => Value2
        )

)