编辑: 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/www/test/sucess.php中为foreach()提供的参数无效
有没有其他方法可以解决它。
先谢谢。
答案 0 :(得分:1)
所以你想得到像
这样的数组'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>";
}
答案 1 :(得分:1)
好像你复制了输出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 />";
}
答案 2 :(得分:0)
$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"
}
}