在PHP中从数组中分离数据


Split data from Array in PHP

编辑:XML内容为:

<params PartnerCD="7882" OrderID="6081833" Timestamp="2016-10-28 05:57:47.303" StatusCD="6" StatusName="Approved - PCB" ChannelCD="2" ChannelName="WEB" OfficeCD="0" OfficeName="All Offices" UserNumber="0" UserName="PnPDBUser" PmtDeviceType="Visa" PmtDeviceLast4="1111" FirstName="test" LastName="t" Address1="test" Address2="NA" City="test" Region="KS" PostalCode="32534" CountryCode="US" PhoneNumber="2344214231" EmailAddress="test@gmail.com" DateOfBirth="" PmtNotes="" OrderTypeCD="1" OrderTypeName="Purchase" OriginalOrderID="NULL" TotalFeeAmount="2.00"  TotalAmountPaid="3.00"  ><product AccountNo="207" LineItemID="138160" Amount="1.00" ProductID="19077" ProductName="Services" Quantity="1" UserID="137" FullName="Suhani Patel" Date="2016-10-28" /></params>

我有如下数组对象:

$example="SimpleXMLElement Object ( [@attributes] => Array ( [PartnerCD] => 7882 [OrderID] => 6081832 [Timestamp] => 2016-10-28 05:35:24.000 [StatusCD] => 6 [StatusName] => Approved - PCB [ChannelCD] => 2 [ChannelName] => WEB [OfficeCD] => 0 [OfficeName] => All Offices [UserNumber] => 0 [UserName] => PnPDBUser [PmtDeviceType] => Visa [PmtDeviceLast4] => 1111 [FirstName] => test [LastName] => t [Address1] => test [Address2] => NA [City] => test [Region] => IA [PostalCode] => 32534 [CountryCode] => US [PhoneNumber] => 2344214231 [EmailAddress] => test@gmail.com [DateOfBirth] => [PmtNotes] => [OrderTypeCD] => 1 [OrderTypeName] => Purchase [OriginalOrderID] => NULL [TotalFeeAmount] => 2.00 [TotalAmountPaid] => 3.00 ) [product] => SimpleXMLElement Object ( [@attributes] => Array ( [AccountNo] => 208 [LineItemID] => 138159 [Amount] => 1.00 [ProductID] => 19077 [ProductName] => Law Library Services [Quantity] => 1 [UserID] => 137 [FullName] => Suhani Patel [Date] => 2016-10-28 ) ) )";

为了获取数组并分配给每个对象(用于数据库操作(,我使用以下代码:

foreach ($example as $mainarray)
{
 $desc =$mainarray["PartnerCD"];
 echo $desc;
 }

但是,我得到了以下错误:

警告:为第2行上/home/www/test/sucess.php中的foreach((提供的参数无效

有其他方法可以解决吗?

提前感谢。

似乎复制了输出SimpleXMLElement等效值的结果。无论如何,以下是您可能需要考虑的内容:

    $xml = new SimpleXMLElement('<params PartnerCD="7882" OrderID="6081833" Timestamp="2016-10-28 05:57:47.303" StatusCD="6" StatusName="Approved - PCB" ChannelCD="2" ChannelName="WEB" OfficeCD="0" OfficeName="All Offices" UserNumber="0" UserName="PnPDBUser" PmtDeviceType="Visa" PmtDeviceLast4="1111" FirstName="test" LastName="t" Address1="test" Address2="NA" City="test" Region="KS" PostalCode="32534" CountryCode="US" PhoneNumber="2344214231" EmailAddress="test@gmail.com" DateOfBirth="" PmtNotes="" OrderTypeCD="1" OrderTypeName="Purchase" OriginalOrderID="NULL" TotalFeeAmount="2.00"  TotalAmountPaid="3.00"  ><product AccountNo="207" LineItemID="138160" Amount="1.00" ProductID="19077" ProductName="Services" Quantity="1" UserID="137" FullName="Suhani Patel" Date="2016-10-28" /></params>');
    var_dump($xml->attributes()['PartnerCD']);  
    var_dump($xml->attributes()['PmtDeviceType']);
    var_dump($xml->attributes()['EmailAddress']);
    var_dump($xml->attributes()['PhoneNumber']);

变体2:

    $xml = new SimpleXMLElement('   <api>
                                        <params PartnerCD="7882" OrderID="6081833" Timestamp="2016-10-28 05:57:47.303" StatusCD="6" StatusName="Approved - PCB" ChannelCD="2" ChannelName="WEB" OfficeCD="0" OfficeName="All Offices" UserNumber="0" UserName="PnPDBUser" PmtDeviceType="Visa" PmtDeviceLast4="1111" FirstName="test" LastName="t" Address1="test" Address2="NA" City="test" Region="KS" PostalCode="32534" CountryCode="US" PhoneNumber="2344214231" EmailAddress="test@gmail.com" DateOfBirth="" PmtNotes="" OrderTypeCD="1" OrderTypeName="Purchase" OriginalOrderID="NULL" TotalFeeAmount="2.00"  TotalAmountPaid="3.00"  ><product AccountNo="207" LineItemID="138160" Amount="1.00" ProductID="19077" ProductName="Services" Quantity="1" UserID="137" FullName="Suhani Patel" Date="2016-10-28" /></params>
                                        <params PartnerCD="7882" OrderID="6081833" Timestamp="2016-10-28 05:57:47.303" StatusCD="6" StatusName="Approved - PCB" ChannelCD="2" ChannelName="WEB" OfficeCD="0" OfficeName="All Offices" UserNumber="0" UserName="PnPDBUser" PmtDeviceType="Visa" PmtDeviceLast4="1111" FirstName="test" LastName="t" Address1="test" Address2="NA" City="test" Region="KS" PostalCode="32534" CountryCode="US" PhoneNumber="2344214231" EmailAddress="test@gmail.com" DateOfBirth="" PmtNotes="" OrderTypeCD="1" OrderTypeName="Purchase" OriginalOrderID="NULL" TotalFeeAmount="2.00"  TotalAmountPaid="3.00"  ><product AccountNo="207" LineItemID="138160" Amount="1.00" ProductID="19077" ProductName="Services" Quantity="1" UserID="137" FullName="Suhani Patel" Date="2016-10-28" /></params>
                                    </api>');
    foreach($xml->children() as $child){
        /**@var SimpleXMLElement $child*/
        echo ($child->xpath("@PartnerCD")[0])                                   . "<br />";
        echo (date("d/m/Y H:i:s", strtotime($child->xpath('@Timestamp')[0])))   . "<br />";
        echo ($child->attributes()['OrderID'])                                  . "<br />";
        echo ($child->attributes()['PhoneNumber'])                              . "<br />";
        echo ($child->attributes()['EmailAddress'])                             . "<br /><br />";
    }

所以你想得到一个类似的数组

'PartnerCD' => 7882,
'OrderID' => 6081832,
...

更新:

$xmlstr = '<params PartnerCD="7882" OrderID="6081833" Timestamp="2016-10-28 05:57:47.303" StatusCD="6" StatusName="Approved - PCB" ChannelCD="2" ChannelName="WEB" OfficeCD="0" OfficeName="All Offices" UserNumber="0" UserName="PnPDBUser" PmtDeviceType="Visa" PmtDeviceLast4="1111" FirstName="test" LastName="t" Address1="test" Address2="NA" City="test" Region="KS" PostalCode="32534" CountryCode="US" PhoneNumber="2344214231" EmailAddress="test@gmail.com" DateOfBirth="" PmtNotes="" OrderTypeCD="1" OrderTypeName="Purchase" OriginalOrderID="NULL" TotalFeeAmount="2.00"  TotalAmountPaid="3.00"  ><product AccountNo="207" LineItemID="138160" Amount="1.00" ProductID="19077" ProductName="Services" Quantity="1" UserID="137" FullName="Suhani Patel" Date="2016-10-28" /></params>';
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->attributes() as $key=>$val) {
  // if($key == "PartnerCD")   <-- this will print just PartnerCD
   echo $key. ": " .$val. "<br>";
}
foreach ($xml->product->attributes() as $key=>$val) {
   echo $key. ": " .$val. "<br>";
}
$xml = <<<XML
<params PartnerCD="7882" OrderID="6081833" 
Timestamp="2016-10-28 05:57:47.303" StatusCD="6" 
    StatusName="Approved - PCB" ChannelCD="2" 
    ChannelName="WEB" OfficeCD="0" OfficeName="All Offices" 
    UserNumber="0" UserName="PnPDBUser" 
    PmtDeviceType="Visa" PmtDeviceLast4="1111" 
    FirstName="test" LastName="t" Address1="test" 
    Address2="NA" City="test" Region="KS" 
    PostalCode="32534" CountryCode="US" 
    PhoneNumber="2344214231" EmailAddress="test@gmail.com" 
    DateOfBirth="" PmtNotes="" OrderTypeCD="1" OrderTypeName="Purchase" 
    OriginalOrderID="NULL" TotalFeeAmount="2.00"  TotalAmountPaid="3.00"  >
        <product AccountNo="207" LineItemID="138160" Amount="1.00" ProductID="19077" ProductName="Services" Quantity="1" UserID="137" FullName="Suhani Patel" Date="2016-10-28" />
</params>
XML;
$example = simplexml_load_string($xml);
foreach ($example as $mainArray) {
    foreach ($mainArray->attributes() as $key => $attribute) {
        $attributesPerDataset[$key] = (string)$attribute;
    }
    $attributes[] = $attributesPerDataset;
}
echo '<pre>';
var_dump($attributes);
exit;

这导致:

array(1) {
  [0]=>
  array(9) {
    ["AccountNo"]=>
    string(3) "207"
    ["LineItemID"]=>
    string(6) "138160"
    ["Amount"]=>
    string(4) "1.00"
    ["ProductID"]=>
    string(5) "19077"
    ["ProductName"]=>
    string(8) "Services"
    ["Quantity"]=>
    string(1) "1"
    ["UserID"]=>
    string(3) "137"
    ["FullName"]=>
    string(12) "Suhani Patel"
    ["Date"]=>
    string(10) "2016-10-28"
  }
}