从复杂的多维数组中访问值

时间:2016-04-16 00:17:39

标签: php arrays object multidimensional-array

我正在使用PHP wrapper API来提升'评级api,而不是ups dev kit直接。我试图回应" MonetaryValue"此数组中包含的运费报价。

试图获得以下价值:

["RatedShipment"][0]["RatedPackage"][0]["TotalCharges"]["MonetaryValue"]

我有点过头,因为我从来没有使用过这种复杂的阵列。然而,它可能是非复杂的。

数组输出:

object(Ups\Entity\RateResponse)#60 (1) { 
    ["RatedShipment"]=> array(1) { 
        [0]=> object(Ups\Entity\RatedShipment)#61 (11) { 
            ["Service"]=> object(Ups\Entity\Service)#62 (3) { 
                ["Description"]=> NULL 
                ["code":"Ups\Entity\Service":private]=> string(2) "03" 
                ["description":"Ups\Entity\Service":private]=> NULL 
            }

            ["RateShipmentWarning"]=> string(56) "Your invoice may vary from the displayed reference rates" 

            ["BillingWeight"]=> object(Ups\Entity\BillingWeight)#68 (2) { 
                ["UnitOfMeasurement"]=> object(Ups\Entity\UnitOfMeasurement)#70 (4) { 
                    ["Code"]=> string(3) "LBS" 
                    ["Description"]=> NULL 
                    ["code":"Ups\Entity\UnitOfMeasurement":private]=> string(3) "LBS" 
                    ["description":"Ups\Entity\UnitOfMeasurement":private]=> NULL 
                }

                ["Weight"]=> string(4) "10.0" 
            }

            ["TransportationCharges"]=> object(Ups\Entity\Charges)#63 (5) { 
                ["CurrencyCode"]=> string(3) "USD" 
                ["MonetaryValue"]=> float(9.84) 
                ["Code"]=> NULL 
                ["Description"]=> NULL 
                ["SubType"]=> NULL 
            } 

            ["ServiceOptionsCharges"]=> object(Ups\Entity\Charges)#65 (5) { 
                ["CurrencyCode"]=> string(3) "USD" 
                ["MonetaryValue"]=> float(0) 
                ["Code"]=> NULL 
                ["Description"]=> NULL 
                ["SubType"]=> NULL 
            } 

            ["TotalCharges"]=> object(Ups\Entity\Charges)#66 (5) { 
                ["CurrencyCode"]=> string(3) "USD" 
                ["MonetaryValue"]=> float(9.84) 
                ["Code"]=> NULL 
                ["Description"]=> NULL 
                ["SubType"]=> NULL 
            } 

            ["GuaranteedDaysToDelivery"]=> NULL 
            ["ScheduledDeliveryTime"]=> NULL 
            ["RatedPackage"]=> array(1) { 
                [0]=> object(Ups\Entity\RatedPackage)#67 (5) { 
                    ["Weight"]=> string(4) "10.0" 
                    ["BillingWeight"]=> object(Ups\Entity\BillingWeight)#74 (2) { 
                        ["UnitOfMeasurement"]=> object(Ups\Entity\UnitOfMeasurement)#76 (4) { 
                            ["Code"]=> string(3) "LBS" 
                            ["Description"]=> NULL 
                            ["code":"Ups\Entity\UnitOfMeasurement":private]=> string(3) "LBS" 
                            ["description":"Ups\Entity\UnitOfMeasurement":private]=> NULL 
                        } 

                        ["Weight"]=> string(4) "10.0" 
                    }

                    ["TransportationCharges"]=> object(Ups\Entity\Charges)#64 (5) { 
                        ["CurrencyCode"]=> string(3) "USD" 
                        ["MonetaryValue"]=> float(9.84) 
                        ["Code"]=> NULL 
                        ["Description"]=> NULL 
                        ["SubType"]=> NULL 
                    } 

                    ["ServiceOptionsCharges"]=> object(Ups\Entity\Charges)#71 (5) { 
                        ["CurrencyCode"]=> string(3) "USD" 
                        ["MonetaryValue"]=> float(0) 
                        ["Code"]=> NULL 
                        ["Description"]=> NULL 
                        ["SubType"]=> NULL 
                    } 

                    ["TotalCharges"]=> object(Ups\Entity\Charges)#72 (5) { 
                        ["CurrencyCode"]=> string(3) "USD" 
                        ["MonetaryValue"]=> float(9.84) 
                        ["Code"]=> NULL 
                        ["Description"]=> NULL 
                        ["SubType"]=> NULL 
                    } 

                } 

            } 

            ["SurCharges"]=> array(0) { } 
            ["NegotiatedRates"]=> NULL 
        } 

    } 

} 

获取上述输出的代码:

$rate = new Ups\Rate(
    $accessKey = '###removed#for#post###',
    $userId = '###removed#for#post###',
    $password = '###removed#for#post###'
);

try {
    $shipment = new \Ups\Entity\Shipment();

    $shipperAddress = $shipment->getShipper()->getAddress();
    $shipperAddress->setPostalCode('99205');

    $address = new \Ups\Entity\Address();
    $address->setPostalCode('99205');
    $shipFrom = new \Ups\Entity\ShipFrom();
    $shipFrom->setAddress($address);

    $shipment->setShipFrom($shipFrom);

    $shipTo = $shipment->getShipTo();
    $shipTo->setCompanyName('Test Ship To');
    $shipToAddress = $shipTo->getAddress();
    $shipToAddress->setPostalCode('99205');

    $package = new \Ups\Entity\Package();
    $package->getPackagingType()->setCode(\Ups\Entity\PackagingType::PT_PACKAGE);
    $package->getPackageWeight()->setWeight(10);

    $dimensions = new \Ups\Entity\Dimensions();
    $dimensions->setHeight(10);
    $dimensions->setWidth(10);
    $dimensions->setLength(10);

    $unit = new \Ups\Entity\UnitOfMeasurement;
    $unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_IN);

    $dimensions->setUnitOfMeasurement($unit);
    $package->setDimensions($dimensions);

    $shipment->addPackage($package);

    var_dump($rate->getRate($shipment));

} catch (Exception $e) {
    var_dump($e);
}

0 个答案:

没有答案