[英]How to pass array values to php variables?
我正在為我們當前的運輸管理系統開發一個非常小的更新。 我應該構建一個php腳本來使用USPS的AddressValidate API來實質上使用給定的地址,並使用USPS進行驗證。 當然,USPS會將格式正確的地址作為數組返回。 我的問題是,如何將返回值轉換為每個字段的變量? (即地址1,地址2,城市等)然后按以下方式回顯結果
地址1: 123 Happyville Lane
城市:匹茲堡
我目前的劇本:(我的詳細信息已被*屏蔽)
<?php
$user = '****';
$xml_data = "<AddressValidateRequest USERID='$user'>" .
"<IncludeOptionalElements>true</IncludeOptionalElements>" .
"<ReturnCarrierRoute>true</ReturnCarrierRoute>" .
"<Address ID='0'>" .
"<FirmName />" .
"<Address1>123 happyville lane</Address1>" .
"<Address2></Address2>" .
"<City>columbus</City>" .
"<State>ohio</State>" .
"<Zip5></Zip5>" .
"<Zip4></Zip4>" .
"</Address>" .
"</AddressValidateRequest>";
$url = "http://production.shippingapis.com/ShippingAPI.dll?API=Verify";
//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Following line is compulsary to add as it is:
curl_setopt($ch, CURLOPT_POSTFIELDS,
'XML=' . $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$output = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
echo PHP_EOL;
返回的數組如下:
Array
(
[Address] => Array
(
[@attributes] => Array
(
[ID] => 0
)
[Address2] => 123 HAPPYVILLE LANE
[City] => COLUMBUS
[State] => OH
[Zip5] => 12345
[Zip4] => 1849
[DeliveryPoint] => 18
[CarrierRoute] => AC016
)
)
echo "Address1: ".$array_data["Address"]["Address2"]."<br>";
echo "City: ".$array_data["Address"]["City"] //wrong city in example :P
應該做的伎倆或如果有更多的地址:
foreach($array_data as $key=>$address){
$address2=$array_data["Address"]["Address2"];//if you need the assignment
echo "Address1: ".$address2."<br>";
echo "City: ".$array_data["Address"]["City"] //wrong city in example :P
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.