dポイントプレゼントキャンペーン実施中!

PHPで作成を行っています。
PEARのXML_Serializerを利用して、XMLをバースし結果を表示したいと考えています。
1つのxmlを利用するのは問題なく終わったのですが、複数のxmlをバースしたい場合の上手い方法が思い付きません。

===↓私が思いついた方法↓====================
$xmlData = 'ttp://example.com/data1.xml';
$xmlData2 = 'ttp://example.com/data2.xml';

$class_xml_uns = &new XML_Unserializer(array('parseAttributes' => true));
$class_xml_uns->unserialize(file_get_contents($xmlData));
$res_list = $class_xml_uns->getUnserializedData();

$class_xml_uns2 = &new XML_Unserializer(array('parseAttributes' => true));
$class_xml_uns2->unserialize(file_get_contents($xmlData2));
$res_list2 = $class_xml_uns2->getUnserializedData();

単純に2回バースするといった感じです。
何か同じ事を2回も3回も書かないといけないし、こんな事をしなくても出来るそうな気がして調べてはみたのですが、検索方法が下手なのか答えに行きつきませんでした。

ご教授とご指導のほど、宜しくお願い致します。

A 回答 (1件)

配列でループさせると下記のようになります。



<?php
$xmlDatas = array(
'ttp://example.com/data1.xml',
'ttp://example.com/data2.xml'
);

$i=0;
foreach ($xmlDatas as $xmlData) {
$class_xml_uns = &new XML_Unserializer(array('parseAttributes' => true));
$class_xml_uns->unserialize(file_get_contents($xmlData));
$res_list[$i] = $class_xml_uns->getUnserializedData();
$i++;
}
?>

ループ後の格納先は、
$res_listが、$res_list[0]で、
$res_list2が、$res_list[1]です。
    • good
    • 0
この回答へのお礼

回答ありがとうございます。

最初、foreachかforでのループ処理…と考えたのですが上手くいきませんでした。
その時は[$i]がを入れていませんでした。
$iに1ずつ足してループさせればよかったんですね!!!
勉強になりました。

お礼日時:2011/01/23 11:11

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!