-1

Would anyone be willing to help me figure out how I can retrieve the franchise name from the second XML page using the franchise id?

I am working with two different pages. The first page will get me a list of the franchise ids. I would like to be able to get the franchise name from the second XML page by using the franchise id from the first page.

Let me know if you need more information, in order to be able to help.

I am looking for an answer in PHP.

PHP: Parsing first XML page

$url = "XMLPAGEURL";
 $xml = simplexml_load_file($url);
  foreach ($xml->franchise as $franchise) {
   echo ''.$franchise[id].'';
  }

Second XML page

<league id="1"/>
 <franchises count="5">
  <franchise name="A" id="0001"/>
  <franchise name="B" id="0002"/>
  <franchise name="C" id="0003"/>
  <franchise name="D" id="0004"/>
  <franchise name="E" id="0005"/>
 </franchises>
</league>
4

1 回答 1

1

您不应该嵌套两组数据的循环,因为第二组中只有一个匹配,用于第一组的当前 id。分别做它们并合并数据是最好的解决方案

<?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第二个循环中,使用 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 />';
}
于 2013-05-14T13:53:31.893 回答