dtstnjl898781429 2013-11-15 09:32
浏览 113

将XML转换为PHP多维数组

I have the following array set in an XML file:

<?xml version="1.0"?>
<hats>
    <a id="Chicago BULLS" img="bulls.jpeg" cost="$25.00" />
    <b id="Toronto RAPTORS" img="raptors.jpeg" cost="$25.00" />
    <c id="Orlando MAGIC" img="magic.jpeg" cost="$25.00" />
</hats>
<clothes>
    <a id="Chicago BULLS 23" img="bullstee.jpeg" cost="$30.00" />
    <b id="Toronto RAPTORS 23" img="torontotee.jpeg" cost="$30.00" />
    <c id="Orlando MAGIC 23" img="magictee.jpeg" cost="$30.00" />
</clothes>

I have used a function I found to convert the XML into a Multidimensional Array which looks like this:

Array
(
    [hats] => Array
        (
            [a] => Array
                (
                )

            [a_attr] => Array
                (
                    [id] => Chicago BULLS
                    [img] => bulls.jpeg
                    [cost] => $25.00
                )

            [b] => Array
                (
                )

            [b_attr] => Array
                (
                    [id] => Toronto RAPTORS
                    [img] => raptors.jpeg
                    [cost] => $25.00
                )

            [c] => Array
                (
                )

            [c_attr] => Array
                (
                    [id] => Orlando MAGIC
                    [img] => magic.jpeg
                    [cost] => $25.00
                )

        )

)

As you can see it has only taken half of the XML file, and also I am unable to use numbers so have to resort to letters.

Ultimately I am trying to have it read the XML file then output only what is shown within the tags and tags and eventually more.

How can I parse the XML file cleanly into an array so I can then use a foreach loop to show each line in a given section of the XML file (hats only or clothes only).

  • 写回答

2条回答 默认 最新

  • douwu8524 2013-11-15 09:37
    关注

    You can use xml_parse_into_struct — Parse XML data into an array structure

    Syntax: int xml_parse_into_struct ( resource $parser , string $data , array &$values [, array &$index ] ) This function parses an XML string into 2 parallel array structures, one (index) containing pointers to the location of the appropriate values in the values array. These last two parameters must be passed by reference.

     <?php
        $simple = '<hats>
                    <a id="Chicago BULLS" img="bulls.jpeg" cost="$25.00" />
                    <b id="Toronto RAPTORS" img="raptors.jpeg" cost="$25.00" />
                    <c id="Orlando MAGIC" img="magic.jpeg" cost="$25.00" />
                </hats>
                <clothes>
                    <a id="Chicago BULLS 23" img="bullstee.jpeg" cost="$30.00" />
                    <b id="Toronto RAPTORS 23" img="torontotee.jpeg" cost="$30.00" />
                    <c id="Orlando MAGIC 23" img="magictee.jpeg" cost="$30.00" />
                </clothes>';
        $p = xml_parser_create();
        xml_parse_into_struct($p, $simple, $vals, $index);
        xml_parser_free($p);
        echo "Index array
    ";
        print_r($index);
        echo "
    Vals array
    ";
        print_r($vals);
      ?>
    

    Ref: http://us1.php.net/xml_parse_into_struct

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错