如何使用PHP将数组xml数据放入表中?

时间:2019-04-17 09:09:40

标签: php xml

我需要有关PHP代码的帮助。我想将通过此查询获得的数组XML数据放入表中。

您能帮我吗?

我的带有XML代码的PHP如下所示:

git push

任何人都知道如何使用PHP将数组XML数据放入表格式。我想要像下面的示例格式图片一样的数组XML输出表:

enter image description here

感谢您的帮助!祝你有美好的一天!

3 个答案:

答案 0 :(得分:0)

您可以申请<table>

<?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['course'])) {
    $fields = array_keys($array['course'][0]);
    unset($fields[0]);
    $fields = array_values($fields);
 $i=0;
?>
<table border="1">
<?php    if (! empty($fields)) { ?>
<tr>
<?php    foreach ($fields as $field) { ?>
<td><?php echo $field;?></td>
<?php  }    ?>
</tr>
<?php    }
if (! empty($array['course'])) {
     foreach ($array['course'] as $elem) {
     unset($elem['@attributes']);
?>
<tr>
<?php    foreach ($elem as $k => $v) { ?>
<td><?php echo $v;?></td>
<?php  }    ?>
</tr>
<?php
        }
    }
}
?>

输出:

<table border="1">
<tr>
    <td>title</td>
    <td>tutor</td>
    <td>duration</td>
    <td>price</td>
</tr>
<tr>
    <td>Java</td>
    <td>Gopal</td>
    <td>3</td>
    <td>$30</td>
</tr>
<tr>
    <td>Hadoop</td>
    <td>Satish</td>
    <td>3</td>
    <td>$50</td>
</tr>
<tr>
    <td>html</td>
    <td>raju</td>
    <td>5</td>
    <td>$50</td>
</tr>
<tr>
    <td>Web Technologies</td>
    <td>Javed</td>
    <td>10</td>
    <td>$60</td>
</tr>

Working Demo:

答案 1 :(得分:0)

简单代码

$array = json_decode(json_encode((array)simplexml_load_string($xml)),true);

答案 2 :(得分:0)

您只需要从数组中获取课程数据,然后简单的foreach即可创建HTML表,请尝试以下代码 检查此在线示例here 输出值 enter image description here

<?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>';


$array = json_decode(json_encode((array)simplexml_load_string($xml)),true);
$array = $array['course'];
?>
<table border="1">
    <thead>
        <tr>
            <td>Title</td>
            <td>Tutor</td>
            <td>Duration</td>
            <td>Price</td>
        </tr>
    </thead>
    <tbody>
<?php
foreach($array as $key => $val){

    echo "<tr>
              <td>".$val['title']."</td>";
        echo "<td>".$val['tutor']."</td>";
        echo "<td>".$val['duration']."</td>";
        echo "<td>".$val['price']."</td>
         </tr>";

}
?>
    </tbody>
</table>