PHP - 如何将SimpleXML解析的结果正确地推送到数组中?

时间:2013-01-29 22:58:53

标签: php arrays xpath simplexml

我正在尝试将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
            )
            .....
        )

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

Array(
    [0] => Array(

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

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

这是你的代码更新;但阅读代码中的注释

$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);