Php 错误的Foreach循环

Php 错误的Foreach循环,php,foreach,cycle,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>"

我的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;

}
$xnl_file=“xml.xml”;
$xml=simplexml\u加载文件($xnl\u文件);
$my_file=0;
foreach($xml作为$value){
var_dump(价值);
$CountryOrganizationId=“”。$xml->Partnership->CountryOrganizationId。“”;
$PartnershipId=“”。$xml->PartnershipId->PartnershipId。“”;
$OwnerId=“”。$xml->Partnership->OwnerId。“”;
$PartnerIdList=“”。$xml->Partnership->PartnerIdList->String。“”;
$CountryOrganizationId_contact=“”。$xml->Partnership->Contract->CountryOrganizationId。“”;
$construcd=“”。$xml->Partnership->Contract->construcd。“”;
$data=“”.$CountryOrganizationId.$PartnershipId.$OwnerId.$PartnerIdList.$CountryOrganizationId\U contact.$ContractId.$Role1.$Category1.$Rate1。
$Role2.$Category2.$Rate2.$Role3.$Category3.$Rate3.“;
回波数据;
}
我从XML中获取数据,并尝试在多个XML上解析它,但这只是一次又一次地复制相同的数据。我不确定我做错了什么。在我看来,数据应该在每一个相同的时间周期中相互重写,但它们不会改变。在echo$data上,我得到了我应该得到的尽可能多的结果,问题是它们是一样的

如果我在开始时var_dump$value,我会得到一个很好的结果,即数据开始循环,但为什么输出总是相同的呢

有人能给我一些建议吗


感谢

Concat$data将其先前的值{$data.=“…”}

foreach($xml作为$value)
{
var_dump(价值);
$CountryOrganizationId=“”。$xml->Partnership->CountryOrganizationId。“”;
$PartnershipId=“”。$xml->PartnershipId->PartnershipId。“”;
$OwnerId=“”。$xml->Partnership->OwnerId。“”;
$PartnerIdList=“”。$xml->Partnership->PartnerIdList->String。“”;
$CountryOrganizationId_contact=“”。$xml->Partnership->Contract->CountryOrganizationId。“”;
$construcd=“”。$xml->Partnership->Contract->construcd。“”;
$data.=''.$CountryOrganizationId.$PartnershipId.$OwnerId.$PartnerIdList.$CountryOrganizationId\U contact.$ContractId.$Role1.$Category1.$Rate1。
$Role2.$Category2.$Rate2.$Role3.$Category3.$Rate3.“;
}
回波数据;

从未使用$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;
}
$xnl_file=“xml.xml”;
$xml=simplexml\u加载文件($xnl\u文件);
$my_file=0;
foreach($xml作为$value){
var_dump(价值);
$CountryOrganizationId=“”。$value->CountryOrganizationId。“”;
$PartnershipId=“”。$value->PartnershipId。“”;
$OwnerId=“”。$value->OwnerId。“”;
$PartnerIdList=“”。$value->PartnerIdList->String。“”;
$CountryOrganizationId_contact=“”。$value->Contract->CountryOrganizationId。“”;
$constractd=“”。$value->Contract->constractd。“”;
$data=”“.$CountryOrganizationId.$PartnershipId.$OwnerId.$PartnerIdList.$CountryOrganizationId\u联系人。
$ContractId.$Role1.$Category1.$Rate1.$Role2.$Category2.$Rate2.$Role3.$Category3.$Rate3。
“afdsf
回波数据;
}

我们想要实现什么?我建议您看看函数
simplexml\u load\u file
返回了什么。如果您在这里发布输出,我们将能够提供更多帮助。但请指定一个问题。看起来您在循环中使用的是$xml而不是$value->Partnership。输出如下:`CZContract58AB4635-D9C6-A04EMM-O-BDD15299MM-O-2A10BCFCZContract58AB4635-D9C6-A04ELabor1250.0Paint2350.0Labor1250.0-----------------------------------------------------CZContract58AB4635-D9C6-A04EMM-O-BDD15299MM-O-2A10BCFCZContract58AB4635-D9C6-A04ELabor1250.0Paint2350.0Labor1250.0-----------------------------------------------------------------------------CZContract58AB4635-D9C6-A04EMM-O-BDD15299MM-O-2A10BCFCZContract58AB4635-D9C6-A04ELabor1250.0Paint2350.0Labor1250.0--------------------------------------------------------------------嘿,谢谢你的回答。当然它有逻辑,我理解你的意思。问题是这个抛出我的错误:注意:尝试获取非对象的属性:(您能发布var_dump($value)的输出吗);我将其扔到pastebin,因为它的文本非常大,无法将其添加到注释中。这个值当然适用于XMLI的每个部分。我已经更新了代码。基本上,当您foreach时,您对每个伙伴关系元素进行foreach,这是循环中的当前元素。您需要替换$value->Partnership->{something\u else}具有$value->{something\u else}。
$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;
}