我需要一些新的想法,我尝试过使用foreach语句和PHP5中的Iteratorts的不同方法。我目前正在解析UPS对多包速率响应的响应。
API返回其所有服务产品,Ground,Next day air等。我想专注于Key Service,这是一个包含' Code'值。一旦找到正确的服务值,就是当我想拉出Array RatedPackages然后遍历它以获取每个包的不同服务及其成本。
我尝试过以下代码但没有用,我宁愿看到不同的方法,在解析数组时我不是主人。最好将数组发送到不同的结构,例如json等。我不知道。
解决方案预期是当服务阵列成员代码 - 01是我需要拉出额定包数组时,我可以生成运输标签。
如果有人有更好的方法,我会向所有人和任何建议开放,如果我不清楚,请问我一个问题,我会尽我所能回应。
提前谢谢
无论如何,这是我的代码:
try {
$object = new ArrayIterator( $arr );
$object->rewind();
while( $object->valid()){
echo $object->key(). "-> ". $object->current()."<br>";
if(is_array($object->current())) {
foreach($object->current() as $node => $value) {
//echo "item: ".$item. " value: ".$value. "<br>";
if($node == 'Service') {
if(is_array($value)) {
s($value->code);
foreach($value as $item => $v){
echo "item: ".$item. " value: ".$v. "<br>";
if($v == '01'){
echo "HIT OUR MARK!!!!!!!";
$rp = $node[0]['RatedPackage'];
s("RP: ".$node);
}
}
}
}
}
}
$object->next();
}
}
catch (Exception $e) {
echo $e->getMessage();
}
代码输出
0-> Array
NULL
item: Code value: 03
1-> Array
NULL
item: Code value: 12
2-> Array
NULL
item: Code value: 02
3-> Array
NULL
item: Code value: 13
4-> Array
NULL
item: Code value: 14
5-> Array
NULL
item: Code value: 01
HIT OUR MARK!!!!!!!
Warning: Illegal string offset 'RatedPackage' in /Applications/MAMP/htdocs/pkdev/dev/portal/shipping.php on line 93
string (11) "RP: Service"
![Snapshot of Array Response][1]
ARRAY to Parse
array(1) [
'RatingServiceSelectionResponse' => array(2) [
'Response' => array(3) [
'TransactionReference' => array(1) [
'CustomerContext' => string (12) "RocketShipIt"
]
'ResponseStatusCode' => string (1) "1"
'ResponseStatusDescription' => string (7) "Success"
]
'RatedShipment' => array(6) [
array(9) [
'Service' => array(1) [
'Code' => string (2) "03"
]
'RatedShipmentWarning' => string (56) "Your invoice may vary from the displayed reference rates"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
'Code' => string (3) "LBS"
]
'Weight' => string (4) "35.0"
]
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "37.12"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "37.12"
]
'GuaranteedDaysToDelivery' => string (0) ""
'ScheduledDeliveryTime' => string (0) ""
'RatedPackage' => array(3) [
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "10.91"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "10.91"
]
'Weight' => string (3) "5.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (3) "5.0"
]
]
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "12.24"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "12.24"
]
'Weight' => string (4) "10.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (4) "10.0"
]
]
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "13.97"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "13.97"
]
'Weight' => string (4) "20.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (4) "20.0"
]
]
]
]
array(9) [
'Service' => array(1) [
'Code' => string (2) "12"
]
'RatedShipmentWarning' => string (56) "Your invoice may vary from the displayed reference rates"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
'Code' => string (3) "LBS"
]
'Weight' => string (4) "35.0"
]
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "59.57"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "59.57"
]
'GuaranteedDaysToDelivery' => string (1) "3"
'ScheduledDeliveryTime' => string (0) ""
'RatedPackage' => array(3) [
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "15.29"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "15.29"
]
'Weight' => string (3) "5.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (3) "5.0"
]
]
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "18.59"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "18.59"
]
'Weight' => string (4) "10.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (4) "10.0"
]
]
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "25.69"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "25.69"
]
'Weight' => string (4) "20.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (4) "20.0"
]
]
]
]
array(9) [
'Service' => array(1) [
'Code' => string (2) "02"
]
'RatedShipmentWarning' => string (56) "Your invoice may vary from the displayed reference rates"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
'Code' => string (3) "LBS"
]
'Weight' => string (4) "35.0"
]
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "77.78"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "77.78"
]
'GuaranteedDaysToDelivery' => string (1) "2"
'ScheduledDeliveryTime' => string (0) ""
'RatedPackage' => array(3) [
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "19.86"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "19.86"
]
'Weight' => string (3) "5.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (3) "5.0"
]
]
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "23.87"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "23.87"
]
'Weight' => string (4) "10.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (4) "10.0"
]
]
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "34.05"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "34.05"
]
'Weight' => string (4) "20.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (4) "20.0"
]
]
]
]
array(9) [
'Service' => array(1) [
'Code' => string (2) "13"
]
'RatedShipmentWarning' => string (56) "Your invoice may vary from the displayed reference rates"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
'Code' => string (3) "LBS"
]
'Weight' => string (4) "35.0"
]
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (6) "121.50"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (6) "121.50"
]
'GuaranteedDaysToDelivery' => string (1) "1"
'ScheduledDeliveryTime' => string (0) ""
'RatedPackage' => array(3) [
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "32.29"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "32.29"
]
'Weight' => string (3) "5.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (3) "5.0"
]
]
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "37.95"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "37.95"
]
'Weight' => string (4) "10.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (4) "10.0"
]
]
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "51.26"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "51.26"
]
'Weight' => string (4) "20.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (4) "20.0"
]
]
]
]
array(9) [
'Service' => array(1) [
'Code' => string (2) "01"
]
'RatedShipmentWarning' => string (56) "Your invoice may vary from the displayed reference rates"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
'Code' => string (3) "LBS"
]
'Weight' => string (4) "35.0"
]
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (6) "139.05"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (6) "139.05"
]
'GuaranteedDaysToDelivery' => string (1) "1"
'ScheduledDeliveryTime' => string (10) "10:30 A.M."
'RatedPackage' => array(3) [
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "37.02"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "37.02"
]
'Weight' => string (3) "5.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (3) "5.0"
]
]
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "43.18"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "43.18"
]
'Weight' => string (4) "10.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (4) "10.0"
]
]
array(5) [
'TransportationCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "58.85"
]
'ServiceOptionsCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (4) "0.00"
]
'TotalCharges' => array(2) [
'CurrencyCode' => string (3) "USD"
'MonetaryValue' => string (5) "58.85"
]
'Weight' => string (4) "20.0"
'BillingWeight' => array(2) [
'UnitOfMeasurement' => array(1) [
*depth too great*
]
'Weight' => string (4) "20.0"
]
]
]
]
]
]
]