错误的Foreach循环

时间:2014-11-06 09:49:39

标签: php foreach cycle

我的foreach周期做错了。但看起来我的知识并不是要弄清楚什么是错的。我的代码非常简单:

   $xnl_file = "xml.xml";
    $xml = simplexml_load_file($xnl_file);
    $my_file = 0;
    foreach ($xml as $value){
        var_dump($value);
        $CountryOrganizationId = "<CountryOrganizationId>".$xml->Partnership->CountryOrganizationId."</CountryOrganizationId>";
        $PartnershipId = "<PartnershipId>".$xml->Partnership->PartnershipId."</PartnershipId>";
        $OwnerId = "<OwnerId>".$xml->Partnership->OwnerId."<OwnerId>";
        $PartnerIdList = "<PartnerIdList><String>".$xml->Partnership->PartnerIdList->String."</String></PartnerIdList>";
        $CountryOrganizationId_contact = "<Contract><CountryOrganizationId>".$xml->Partnership->Contract->CountryOrganizationId."</CountryOrganizationId>";
        $ContractId = "<ContractId>".$xml->Partnership->Contract->ContractId."</ContractId>";
        $data = "<Partnership>".$CountryOrganizationId.$PartnershipId.$OwnerId.$PartnerIdList.$CountryOrganizationId_contact.$ContractId.$Role1.$Category1.$Rate1.
        $Role2.$Category2.$Rate2.$Role3.$Category3.$Rate3."</Partnership>";
        echo $data;

}

我从XML获取数据并尝试在多个数据上解析它,但这只是一次又一次地复制相同的数据。我不确定我做错了什么。在我看来,数据应该在每个周期都相同时重写,但它们不会改变。在echo $ data中,我得到尽可能多的结果,问题就是它们是相同的。

如果我在开始时获得var_dump $ value,那么数据会变得很好,但是为什么输出始终是相同的?

请有人可以给我建议吗?

由于

2 个答案:

答案 0 :(得分:1)

$ value变量从不使用,你总是使用$ xml。试试吧:

$xnl_file = "xml.xml";
$xml = simplexml_load_file($xnl_file);
$my_file = 0;
foreach ($xml as $value){
    var_dump($value);
    $CountryOrganizationId         = "<CountryOrganizationId>" . $value->CountryOrganizationId . "</CountryOrganizationId>";
    $PartnershipId                 = "<PartnershipId>" . $value->PartnershipId . "</PartnershipId>";
    $OwnerId                       = "<OwnerId>" . $value->OwnerId . "<OwnerId>";
    $PartnerIdList                 = "<PartnerIdList><String>" . $value->PartnerIdList->String . "</String></PartnerIdList>";
    $CountryOrganizationId_contact = "<Contract><CountryOrganizationId>" . $value->Contract->CountryOrganizationId . "</CountryOrganizationId>";
    $ContractId                    = "<ContractId>" . $value->Contract->ContractId . "</ContractId>";
    $data                          = "<Partnership>" . $CountryOrganizationId . $PartnershipId . $OwnerId . $PartnerIdList . $CountryOrganizationId_contact . 
                                     $ContractId . $Role1 . $Category1 . $Rate1 . $Role2 . $Category2 . $Rate2 . $Role3 . $Category3 . $Rate3 . 
                                    "</Partnership>"afdsf

    echo $data;
}

答案 1 :(得分:-2)

Concat $数据到之前的值{$ data。=“......”}

foreach ($xml as $value)
{
        var_dump($value);
        $CountryOrganizationId = "<CountryOrganizationId>".$xml->Partnership->CountryOrganizationId."</CountryOrganizationId>";
        $PartnershipId = "<PartnershipId>".$xml->Partnership->PartnershipId."</PartnershipId>";
        $OwnerId = "<OwnerId>".$xml->Partnership->OwnerId."<OwnerId>";
        $PartnerIdList = "<PartnerIdList><String>".$xml->Partnership->PartnerIdList->String."</String></PartnerIdList>";
        $CountryOrganizationId_contact = "<Contract><CountryOrganizationId>".$xml->Partnership->Contract->CountryOrganizationId."</CountryOrganizationId>";
        $ContractId = "<ContractId>".$xml->Partnership->Contract->ContractId."</ContractId>";

        $data .= "<Partnership>".$CountryOrganizationId.$PartnershipId.$OwnerId.$PartnerIdList.$CountryOrganizationId_contact.$ContractId.$Role1.$Category1.$Rate1.
        $Role2.$Category2.$Rate2.$Role3.$Category3.$Rate3."</Partnership>";
}
 echo $data;