如何使用PHP将XML转换为数组?

时间:2019-04-17 06:12:41

标签: php arrays xml

我正在尝试在数组中拆分XML,以下是我的php代码:    

  <?php
     $xml = simplexml_load_file("test.xml") or die("Error: Cannot create object");

     foreach($xml->children() as $books) { 
        echo $books->title . "<br> "; 
        echo $books->tutor . "<br> "; 
        echo $books->duration . "<br> ";
        echo $books->price . "<hr>"; 


     }
  ?>

下面是我的XML代码:

<?xml version = "1.0" encoding = "utf-8"?>
<tutorialspoint>

<course category = "JAVA">
  <title lang = "en">Java</title>
  <tutor>Gopal</tutor>
  <duration>3</duration>
  <price>$30</price>
</course>

<course category = "HADOOP">
  <title lang = "en">Hadoop</title>.
  <tutor>Satish</tutor>
  <duration>3</duration>
  <price>$50</price>
</course>

<course category = "HTML">
  <title lang = "en">html</title>
  <tutor>raju</tutor>
  <duration>5</duration>
  <price>$50</price>
</course>

<course category = "WEB">
  <title lang = "en">Web Technologies</title>
  <tutor>Javed</tutor>
  <duration>10</duration>
  <price>$60</price>
</course>

</tutorialspoint>

但是它在下面显示了输出: enter image description here

我想用PHP将XML转换为数组,但是不能工作。实际上,我想要的输出如以下示例代码所示:

Array
(
[0] => Array
    (
        [title] => Java
        [tutor] => Gopal
        [duration] => 3
        [price] => $30
    )

[1] => Array
    (
        [title] => Hadoop
        [tutor] => Satish
        [duration] => 3
        [price] => $50
    )

[2] => Array
    (
        [title] => HTML
        [tutor] => raju
        [duration] => 5
        [price] => $50
    )

[3] => Array
    (
        [title] => Web Technologies
        [tutor] => Javed
        [duration] => 10
        [price] => $60
    )

我不知道如何像上面的示例输出一样将它们设置到数组中。希望可以有人帮帮我。谢谢。

2 个答案:

答案 0 :(得分:0)

此代码段会将您的XML转换为数组

$array = json_decode(json_encode((array)simplexml_load_string($xml)),true);
echo '<pre>';
print_r($array);

答案 1 :(得分:0)

您可以先json_encode()然后json_decode()作为数组并使用simplexml_load_string()

步骤:

1)首先使用simplexml_load_string()将XML转换为可读的字符串对象。

2)然后json_encode()

3)json_decode(),带有第二个参数TRUE,它将返回数组而不是对象。

4)现在,您的XML被转换为数组。

5)取一个空白数组,从上面的代码中循环遍历数组,并将元素附加到其中。

要获得所需的输出:

<?php
$xml = '<?xml version = "1.0" encoding = "utf-8"?>
<tutorialspoint>

<course category = "JAVA">
  <title lang = "en">Java</title>
  <tutor>Gopal</tutor>
  <duration>3</duration>
  <price>$30</price>
</course>

<course category = "HADOOP">
  <title lang = "en">Hadoop</title>.
  <tutor>Satish</tutor>
  <duration>3</duration>
  <price>$50</price>
</course>

<course category = "HTML">
  <title lang = "en">html</title>
  <tutor>raju</tutor>
  <duration>5</duration>
  <price>$50</price>
</course>

<course category = "WEB">
  <title lang = "en">Web Technologies</title>
  <tutor>Javed</tutor>
  <duration>10</duration>
  <price>$60</price>
</course>
</tutorialspoint>';

$arr = [];
$array = json_decode(json_encode(simplexml_load_string($xml)),true);
if ( ! empty($array)) {
 $i=0;
 foreach ($array['course'] as $elem) {
   $arr[$i]['title'] = $elem['title'];
   $arr[$i]['tutor'] = $elem['tutor'];
   $arr[$i]['duration'] = $elem['duration'];
   $arr[$i]['price'] = $elem['price'];
  ++$i;
 }
}
echo '<pre>';print_r($arr);echo '</pre>';

输出:

Array
(
    [0] => Array
        (
            [title] => Java
            [tutor] => Gopal
            [duration] => 3
            [price] => $30
        )

    [1] => Array
        (
            [title] => Hadoop
            [tutor] => Satish
            [duration] => 3
            [price] => $50
        )

    [2] => Array
        (
            [title] => html
            [tutor] => raju
            [duration] => 5
            [price] => $50
        )

    [3] => Array
        (
            [title] => Web Technologies
            [tutor] => Javed
            [duration] => 10
            [price] => $60
        )

)

Working Code: