我有这个结果,我想提取一些信息,如service-name, 价格,预计交货日期...... 如何正确地循环并提取这些信息。
由于
Array
(
[price-quotes] => Array
(
[price-quote] => Array
(
[0] => Array
(
[service-code] => DOM.EP
[service-link] => Array
(
[@value] =>
[@attributes] => Array
(
[rel] => service
[href] => https://ct.soa-gw.canadapost.ca/rs/ship/service/DOM.EP?country=CA
[media-type] => application/vnd.cpc.ship.rate-v3+xml
)
)
[service-name] => Expedited Parcel
[price-details] => Array
(
[base] => 8.20
[taxes] => Array
(
[gst] => Array
(
[@value] => 0.41
[@attributes] => Array
(
[percent] => 5
)
)
[pst] => Array
(
[@value] => 0.82
[@attributes] => Array
(
[percent] => 9.975
)
)
[hst] => 0
)
[due] => 9.42
[options] => Array
(
[option] => Array
(
[option-code] => DC
[option-name] => Delivery confirmation
[option-price] => 0
)
)
[adjustments] => Array
(
[adjustment] => Array
(
[0] => Array
(
[adjustment-code] => FUELSC
[adjustment-name] => Fuel surcharge
[adjustment-cost] => 0.24
[qualifier] => Array
(
[percent] => 3
)
)
[1] => Array
(
[adjustment-code] => V1DISC
[adjustment-name] => SMB Savings
[adjustment-cost] => -0.25
)
)
)
)
[weight-details] =>
[service-standard] => Array
(
[am-delivery] =>
[guaranteed-delivery] => 1
[expected-transit-time] => 1
[expected-delivery-date] => 1983
)
)
[1] => Array
(
[service-code] => DOM.PC
[service-link] => Array
(
[@value] =>
[@attributes] => Array
(
[rel] => service
[href] => https://ct.soa-gw.canadapost.ca/rs/ship/service/DOM.PC?country=CA
[media-type] => application/vnd.cpc.ship.rate-v3+xml
)
)
[service-name] => Priority
[price-details] => Array
(
[base] => 16.86
[taxes] => Array
(
[gst] => Array
(
[@value] => 0.88
[@attributes] => Array
(
[percent] => 5
)
)
[pst] => Array
(
[@value] => 1.76
[@attributes] => Array
(
[percent] => 9.975
)
)
[hst] => 0
)
[due] => 20.3
[options] => Array
(
[option] => Array
(
[option-code] => DC
[option-name] => Delivery confirmation
[option-price] => 0
)
)
[adjustments] => Array
(
[adjustment] => Array
(
[0] => Array
(
[adjustment-code] => FUELSC
[adjustment-name] => Fuel surcharge
[adjustment-cost] => 1.31
[qualifier] => Array
(
[percent] => 8
)
)
[1] => Array
(
[adjustment-code] => V1DISC
[adjustment-name] => SMB Savings
[adjustment-cost] => -0.51
)
)
)
)
[weight-details] =>
[service-standard] => Array
(
[am-delivery] =>
[guaranteed-delivery] => 1
[expected-transit-time] => 1
[expected-delivery-date] => 1983
)
)
[2] => Array
(
[service-code] => DOM.RP
[service-link] => Array
(
[@value] =>
[@attributes] => Array
(
[rel] => service
[href] => https://ct.soa-gw.canadapost.ca/rs/ship/service/DOM.RP?country=CA
[media-type] => application/vnd.cpc.ship.rate-v3+xml
)
)
[service-name] => Regular Parcel
[price-details] => Array
(
[base] => 8.2
[taxes] => Array
(
[gst] => Array
(
[@value] => 0.41
[@attributes] => Array
(
[percent] => 5
)
)
[pst] => Array
(
[@value] => 0.82
[@attributes] => Array
(
[percent] => 9.975
)
)
[hst] => 0
)
[due] => 9.42
[options] => Array
(
[option] => Array
(
[option-code] => DC
[option-name] => Delivery confirmation
[option-price] => 0
[qualifier] => Array
(
[included] => 1
)
)
)
[adjustments] => Array
(
[adjustment] => Array
(
[0] => Array
(
[adjustment-code] => FUELSC
[adjustment-name] => Fuel surcharge
[adjustment-cost] => 0.24
[qualifier] => Array
(
[percent] => 3
)
)
[1] => Array
(
[adjustment-code] => V1DISC
[adjustment-name] => SMB Savings
[adjustment-cost] => -0.25
)
)
)
)
[weight-details] =>
[service-standard] => Array
(
[am-delivery] =>
[guaranteed-delivery] =>
[expected-transit-time] => 2
[expected-delivery-date] => 1982
)
)
[3] => Array
(
[service-code] => DOM.XP
[service-link] => Array
(
[@value] =>
[@attributes] => Array
(
[rel] => service
[href] => https://ct.soa-gw.canadapost.ca/rs/ship/service/DOM.XP?country=CA
[media-type] => application/vnd.cpc.ship.rate-v3+xml
)
)
[service-name] => Xpresspost
[price-details] => Array
(
[base] => 9.96
[taxes] => Array
(
[gst] => Array
(
[@value] => 0.52
[@attributes] => Array
(
[percent] => 5
)
)
[pst] => Array
(
[@value] => 1.04
[@attributes] => Array
(
[percent] => 9.975
)
)
[hst] => 0
)
[due] => 11.99
[options] => Array
(
[option] => Array
(
[option-code] => DC
[option-name] => Delivery confirmation
[option-price] => 0
)
)
[adjustments] => Array
(
[adjustment] => Array
(
[0] => Array
(
[adjustment-code] => FUELSC
[adjustment-name] => Fuel surcharge
[adjustment-cost] => 0.77
[qualifier] => Array
(
[percent] => 8
)
)
[1] => Array
(
[adjustment-code] => V1DISC
[adjustment-name] => SMB Savings
[adjustment-cost] => -0.3
)
)
)
)
[weight-details] =>
[service-standard] => Array
(
[am-delivery] =>
[guaranteed-delivery] => 1
[expected-transit-time] => 1
[expected-delivery-date] => 1983
)
)
)
)
)
提取代码的示例 $ preserve_keys:(0 =>从不,1 =>字符串,2 =>总是)
function array_flatten($array, $preserve_keys = 1, &$newArray = Array()) {
foreach ($array as $key => $child) {
if (is_array($child)) {
$newArray =& array_flatten($child, $preserve_keys, $newArray);
} elseif ($preserve_keys + is_string($key) > 1) {
$newArray[$key] = $child;
} else {
$newArray[] = $child;
}
}
return $newArray;
}
print_r( '<br>' );
$test = array_flatten($result, 1);
echo $test;
答案 0 :(得分:1)
你必须循环价格报价。
foreach($array['price-quotes']['price-quote'] as $key=>$value){
echo '<br/>service-name : '.$value['service-code'];
echo '<br/>price : '.$value['price-details']['due'];
echo '<br/>expected-delivery-date : '.$value['service-standard']['expected-delivery-date'];
echo "<hr/>";
// or you can create an array
$new_array[$key]['service-name'] = $value['service-code'];
$new_array[$key]['price'] = $value['price-details']['due'];
$new_array[$key]['expected-delivery-date'] = $value['service-standard']['expected-delivery-date'];
}
输出:
service-name : DOM.EP
price : 9.42
expected-delivery-date : 1983
===============
service-name : DOM.PC
price : 20.3
expected-delivery-date : 1983
===============
service-name : DOM.RP
price : 9.42
expected-delivery-date : 1982
===============
service-name : DOM.XP
price : 11.99
expected-delivery-date : 1983
===============