【问题标题】:PHP - how to push the result of SimpleXML parsing correctly into the Array?PHP - 如何将 SimpleXML 解析的结果正确推送到数组中?
【发布时间】:2013-01-29 22:58:53
【问题描述】:

我正在尝试将 xml 解析的结果推送到多维数组。 XML 的结构是here。 PHP 程序是(不能正常工作):

$xml_url = simplexml_load_file("url to the xml document");
$data = $xml_url->xpath('MULTIPLE/SINGLE/KEY');

$current = 0;
$topics_list = array();

//display list of 'chapters'
foreach($data as $chap_name) {
    if ($chap_name['name'] == 'name') {
        echo $chap_name->VALUE . '<br />';
        $topics_list[$current]['chapter'] = $chap_name->VALUE;
    }
}

$data2 = $xml_url->xpath('MULTIPLE/SINGLE/KEY[@name="modules"]/MULTIPLE/SINGLE/KEY');

//display list of 'topics' & 'contents'
foreach($data2 as $topic_name) {
    if ($topic_name['name'] == 'name') {
        echo $topic_name->VALUE . '<br />';
        $topics_list[$current]['topic'] = $topic_name->VALUE;
    }
    if ($topic_name['name'] == 'description') {
        echo $topic_name->VALUE . '<br />';
        $topics_list[$current]['content'] = $topic_name->VALUE;
    }
}
print_r($topics_list);

我要推送数据的Array的结构是:

Array (
            [0] => Array (
                    [chapter] => Chapter_name1
                    [name] => Topic_name1
                    [content] => Content_of_the_topic1
                )
            [1] => Array (
                    [chapter] => Chapter_name1
                    [name] => Topic_name2
                    [content] => Content_of_the_topic2
                )
            [2] => Array (
                    [chapter] => Chapter_name2
                    [name] => Topic_name2
                    [content] => Content_of_the_topic2
            )
            .....
        )

更新:这是上述代码处理的结果:

Array(
    [0] => Array(

        [chapter] => SimpleXMLElement Object
            (
                [0] => STÖRUNGEN
            )

        [topic] => SimpleXMLElement Object
            (
                [0] => 3.25 Starke Blutung
            )

        [content] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [null] => null
                    )
            )
    )

)

【问题讨论】:

  • 在什么情况下不能正常工作?
  • 我没有看到你在任何地方增加$current。它在整个循环中保持为 0。
  • @Jim 查看我的更新...我只想避免 SimpleXMLElement Object 并将所有数据放入数组中,现在只保存最后一个值
  • @MichaelBerkowski 我是 php 和 xml 解析的新手,你能告诉我,我需要在哪里增加它吗?谢谢
  • 将 $chap_name->VALUE 包装在 get_object_vars() 中,看看是否可行。 Michael 的评论是说您将循环遍历 XML 数据,因此您的主题将 Chapter 会改变,但您不会增加 $current,因此每次都会覆盖它。随着章节名称的每次迭代,您需要增加 $current

标签: php arrays xpath simplexml


【解决方案1】:

尝试将 SimpleXMLElement 中的值“转换”为字符串,然后将其添加到数组中,如下所示:

// add (string) after the '='
$topics_list[$current]['content'] = (string) $topic_name->VALUE;

这将确保 SimpleXMLElement 元素的 被添加为字符串,而不是 n 对象。

另外(由其他人标记),确保增加 $current 以防止每条记录覆盖前一条

您的代码已更新;但是阅读代码中的cmets

$xml_url = simplexml_load_file("url to the xml document");
$data  = $xml_url->xpath('MULTIPLE/SINGLE/KEY');
$data2 = $xml_url->xpath('MULTIPLE/SINGLE/KEY[@name="modules"]/MULTIPLE/SINGLE/KEY');

$current = 0;
$topics_list = array();

//display list of 'chapters'
foreach($data as $chap_name) {
    if ($chap_name['name'] == 'name') {
        $topics_list[$current]['chapter'] = (string) $chap_name->VALUE;
    }

    // display list of 'topics' & 'contents'
    // NOTE: Not sure if this will work as expected, based on
    //       the name 'topic', I suspect a single chapter
    //       will have *multiple* topics.
    //       Also, those 'topics' will probably need to be 'matched'
    //       to the current 'chapter', so that only the topics for
    //       *this* chapter will be added here!
    foreach($data2 as $topic_name) {
        if ($topic_name['name'] == 'name') {
            $topics_list[$current]['topic'] = (string) $topic_name->VALUE;
        }
        if ($topic_name['name'] == 'description') {
            $topics_list[$current]['content'] = (string) $topic_name->VALUE;
        }
    }

    $current++;
}

print_r($topics_list);

【讨论】:

  • 谢谢,它可以按我的意愿工作,但是我想我需要将所有条件放入一个 foreach 循环中才能正确增加 $current。顺便问一下,你知道怎么做吗?
  • @Dozent 我已经用一个完整的例子更新了我的答案,但是 阅读代码中的 cmets 因为我不确定这是否会完全按预期工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
  • 2022-11-21
  • 1970-01-01
  • 2019-11-02
  • 2014-02-03
  • 1970-01-01
相关资源
最近更新 更多