命名空间对象到 select 列表 php

Namespaced object to select list php

我正尝试在我的网站上使用 api by https://github.com/101medialab/shipping 的联邦快递运输。当我尝试代码

时,我不擅长 php

$calculator = new MediaLab\Shipping\Calculator\FedExCalculator($key,$password, $accountNumber, $meterNumber); $calculator->calculate($source, $destination, $shipment);

我可以看到这样的回复:

`

Array
(
    [0] => MediaLab\Shipping\Model\Estimation Object
        (
            [carrier:MediaLab\Shipping\Model\Estimation:private] => FedEx
            [serviceName:MediaLab\Shipping\Model\Estimation:private] => First overnight
            [serviceCode:MediaLab\Shipping\Model\Estimation:private] => FIRST_OVERNIGHT
            [deliveryDate:MediaLab\Shipping\Model\Estimation:private] => DateTime Object
                (
                    [date] => 2015-07-27 08:00:00
                    [timezone_type] => 3
                    [timezone] => Europe/Paris
                )

            [cost:MediaLab\Shipping\Model\Estimation:private] => MediaLab\Shipping\Model\Cost Object
                (
                    [currency:MediaLab\Shipping\Model\Cost:private] => USD
                    [amount:MediaLab\Shipping\Model\Cost:private] => 161.59
                )

        )

    [1] => MediaLab\Shipping\Model\Estimation Object
        (
            [carrier:MediaLab\Shipping\Model\Estimation:private] => FedEx
            [serviceName:MediaLab\Shipping\Model\Estimation:private] => Priority overnight
            [serviceCode:MediaLab\Shipping\Model\Estimation:private] => PRIORITY_OVERNIGHT
            [deliveryDate:MediaLab\Shipping\Model\Estimation:private] => DateTime Object
                (
                    [date] => 2015-07-27 10:30:00
                    [timezone_type] => 3
                    [timezone] => Europe/Paris
                )

            [cost:MediaLab\Shipping\Model\Estimation:private] => MediaLab\Shipping\Model\Cost Object
                (
                    [currency:MediaLab\Shipping\Model\Cost:private] => USD
                    [amount:MediaLab\Shipping\Model\Cost:private] => 71.65
                )

        )
)

` 但我对如何让这些值显示在 select 列表中感到困惑。非常感谢任何帮助。

下面应该建立一个select框来显示结果:

$calculator = new MediaLab\Shipping\Calculator\FedExCalculator($key,$password, $accountNumber, $meterNumber);
$results = $calculator->calculate($source, $destination, $shipment);

var $html = '<select>';
foreach ($results as $result) {
    $cost = $result->getCost()->getCurrency() . $result->getCost()->getAmount();
    $html .= '<option name="' . $result->getServiceCode() . '">' . $result->getCarrier() . ' - ' . $result->getServiceName() . ' - ' . $result->getDeliveryDate() . ' - ' . $cost . '</option>';
}
$html = '</select>';
echo $html;

基本上,您循环遍历结果并使用对象提供的方法来获取您需要的数据。查看 https://github.com/101medialab/shipping/blob/master/src/MediaLab/Shipping/Model/EstimationInterface.php 处的 class 接口对所有可用方法可能会有帮助。