2014-11-06 107 views
0

我在做我的foreach循环有问题。但看起来我的知识并不能解决问题。我的代码非常简单:错误的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获取数据,并尝试分析它多一个,但是这只是一次又一次地复制相同的数据。我不知道我做错了什么。在我看来,数据应该在每次循环做相同的时候相互重写,但它们并没有改变。在回声$数据我得到尽可能多的结果,我应该,问题只是他们是一样的。

如果我在开始var_dump $值,我得到很好的结果,数据来周期,但为什么输出是一直都是一样的?

请有人给我建议吗?

感谢

+1

什么是试图实现?我建议你看看函数'simplexml_load_file'返回的内容。如果你在这里发布输出,我们会更有帮助。但请说明一个问题。 – 2014-11-06 09:52:06

+2

看起来您正在使用$ xml而不是$ value->您的循环中的Partnership。 – 2014-11-06 09:52:59

+0

输出是这样的: 'CZContract_58AB4635-D9C6-A04EMM-O-BDD15299MM-O-2A10BCFCZContract_58AB4635-D9C6-A04ELabor1250.0Paint2350.0Labor1250.0 ----------------- -------------------------------------------------- - CZContract_58AB4635-D9C6-A04EMM-O-BDD15299MM-O-2A10BCFCZContract_58AB4635-D9C6-A04ELabor1250.0Paint2350.0Loror1250.0 ----------------------- --------------------------------------------- CZContract_58AB4635-D9C6 -A04EMM-O-BDD15299MM-O-2A10BCFCZContract_58AB4635-D9C6-A04ELabor1250.0Paint2350.0Loror1250.0 ----------------------------- --------------------------------------- – Andurit 2014-11-06 09:53:36

回答

1

的$值变量从未使用过,你总是使用$ 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; 
} 
+0

嘿,谢谢你的回答。当然它有逻辑,我明白你的意思。问题是,这扔我错误: 注意:试图获得非对象的属性:( – Andurit 2014-11-06 10:00:08

+0

可以请您发布var_dump($ value)的输出; – motanelu 2014-11-06 10:01:17

+0

http://pastebin.com/VM4EaDh7 我把它扔掉因为它的相当大的文本,并且无法将它添加到评论中 这个值当然是XML的每个部分 – Andurit 2014-11-06 10:04:23

-2

的毗连$数据到以前的值{$数据= “...”}

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;