2014-11-06 111 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;