我有一个字符串形式的 XML(在 XLS 转换之后):
<course>
<topic>
<chapter>Some value</chapter>
<title>Some value</title>
<content>Some value</content>
</topic>
<topic>
<chapter>Some value</chapter>
<title>Some value</title>
<content>Some value</content>
</topic>
....
</course>
然后我将上述 XML 推送到 Array() 中:
$new_xml = $proc->transformToXML($xml);
$xml2 = simplexml_load_string($new_xml);
$root = $xml2->xpath("//topic");
$current = 0;
$topics_list = array();
// put the xml values into multidimensional array
foreach($root as $data) {
if ($data === 'chapter') {
$topics_list[$current]['chapter'] = $data->chapter;
}
if ($data === 'title') {
$topics_list[$current]['title'] = $data->title;
}
if ($data === 'content') {
$topics_list[$current]['content'] = $data->content;
}
$current++;
}
print_r($topics_list);
问题:结果是空数组。我试过像这样的字符串:
$topics_list[$current]['chapter'] = (string) $data->chapter;
但结果仍然是空的。谁能解释一下,我的错误在哪里。谢谢。