最初の現在の ID に対して、2 番目のセットには 1 つの一致しかないため、両方のデータ セットのループをネストしないでください。それらを別々に行い、データをマージすることが最善の解決策です
<?php
$data=array();
$url1 = "http://football99.myfantasyleague.com/2007/export?TYPE=standings&L=46184&XML=1";
$xml = simplexml_load_file($url1);
foreach ($xml->franchise as $franchise)
{
$id=(string) $franchise->attributes()->id;
$data[$id]['id']=$id;
foreach($franchise as $key=>$value)
{
$data[$id][$key]=(string) $value;
}
}
$url2 = "http://football99.myfantasyleague.com/2007/export?TYPE=league&L=46184&XML=1";
$xml_second = simplexml_load_file($url2);
foreach($xml_second->franchises->franchise as $franchise_sec)
{
//var_dump($franchise_sec);
$id=(string) $franchise_sec->attributes()->id;
foreach($franchise_sec->attributes() as $key=>$value)
{
$data[$id][$key]=(string) $value;
}
}
print_r($data);
これはid
、最初のループのデータとname
2 番目のループのデータに入れ、両方のインデックスとして id を使用します (これは共通の要素であるため)
他のデータも追加しようとしました
コードの変更が必要であることがテストで証明されたため、更新
生成された出力は:-
Array
(
[0009] => Array
(
[id] => 0009
[h2hw] => 12
[name] => Hindenberg
[logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0051.gif
)
[0004] => Array
(
[id] => 0004
[h2hw] => 9
[name] => Skins
[logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0837.gif
)
[0010] => Array
(
[id] => 0010
[h2hw] => 9
[name] => Marooned
[logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0841.gif
)
[0003] => Array
(
[id] => 0003
[h2hw] => 7
[name] => Elf Boys
[logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0703.gif
)
[0007] => Array
(
[id] => 0007
[h2hw] => 7
[name] => Juggernaut
[logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0689.gif
)
[0006] => Array
(
[id] => 0006
[h2hw] => 7
[name] => Knights
[logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0708.gif
)
[0002] => Array
(
[id] => 0002
[h2hw] => 7
[name] => Dr. Death
[logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0829.gif
)
[0005] => Array
(
[id] => 0005
[h2hw] => 6
[name] => Busted Season
[logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0842.gif
)
[0001] => Array
(
[id] => 0001
[h2hw] => 4
[name] => Deep Divot
[logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0826.gif
)
[0008] => Array
(
[id] => 0008
[h2hw] => 3
[name] => Last Place
[logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0827.gif
)
)
次のようにデータを取得できます。
while(list($key,$franchise)=each($data))
{
echo $franchise['id'].': '.$franchise['name'].' <img src="'.$franchise['logo'].'"/><br />';
}