xml 解析の結果を多次元配列にプッシュしようとしています。XMLの構造はこちら。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 (
[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
)
)
)
)