解析PHP对象中的xml数组响应

时间:2014-05-20 10:24:38

标签: php arrays xml json

我正在研究UPS跟踪API,我已经调用API来跟踪包,我得到了正确的响应,但是在数组格式中,我是json解码的新手,请告诉我如何在PHP中解析它,我必须在PHP中创建多个对象吗?我附加输出以及PHP代码。

PHP代码

if (isset($_POST['af0'])) {
    if (preg_match('/^[a-z\d_]{4,80}$/i', $_POST['trackingNumber'])) {
        $cleanTrackingNumber = $_POST['trackingNumber'];
        $someArray = upsTrack("$cleanTrackingNumber");
        echo '<pre>'; print_r($someArray); echo '</pre>';

    } else {
        echo 'Invalid tracking number... sigh...';
    }
}

输出

Array
(
    [TRACKRESPONSE] => Array
        (
            [RESPONSE] => Array
                (
                    [TRANSACTIONREFERENCE] => Array
                        (
                            [XPCIVERSION] => 1.0
                        )

                    [RESPONSESTATUSCODE] => 1
                    [RESPONSESTATUSDESCRIPTION] => Success
                )

            [SHIPMENT] => Array
                (
                    [SHIPPER] => Array
                        (
                            [SHIPPERNUMBER] => A6161A
                            [ADDRESS] => Array
                                (
                                    [ADDRESSLINE1] => 132 E 43RD ST
                                    [CITY] => NEW YORK
                                    [STATEPROVINCECODE] => NY
                                    [POSTALCODE] => 10017   4019
                                    [COUNTRYCODE] => US
                                )

                        )

                    [SHIPTO] => Array
                        (
                            [ADDRESS] => Array
                                (
                                    [CITY] => TORONTO
                                    [STATEPROVINCECODE] => ON
                                    [POSTALCODE] => M5V3X1
                                    [COUNTRYCODE] => CA
                                )

                        )

                    [SHIPMENTWEIGHT] => Array
                        (
                            [UNITOFMEASUREMENT] => Array
                                (
                                    [CODE] => LBS
                                )

                            [WEIGHT] => 3.20
                        )

                    [SERVICE] => Array
                        (
                            [CODE] => 011
                            [DESCRIPTION] => STANDARD
                        )

                    [REFERENCENUMBER] => Array
                        (
                            [CODE] => 13
                            [VALUE] => A6161AD9HPK
                        )

                    [SHIPMENTIDENTIFICATIONNUMBER] => 1ZA6161A6832763249
                    [PICKUPDATE] => 20140519
                    [SCHEDULEDDELIVERYDATE] => 20140521
                    [PACKAGE] => Array
                        (
                            [TRACKINGNUMBER] => 1ZA6161A6832763249
                            [ACTIVITY] => Array
                                (
                                    [ACTIVITYLOCATION] => Array
                                        (
                                            [ADDRESS] => Array
                                                (
                                                    [CITY] => SECAUCUS
                                                    [STATEPROVINCECODE] => NJ
                                                    [COUNTRYCODE] => US
                                                )

                                        )

                                    [STATUS] => Array
                                        (
                                            [STATUSTYPE] => Array
                                                (
                                                    [CODE] => I
                                                    [DESCRIPTION] => DEPARTURE SCAN
                                                )

                                            [STATUSCODE] => Array
                                                (
                                                    [CODE] => DP
                                                )

                                        )

                                    [DATE] => 20140520
                                    [TIME] => 053000
                                )

                            [MESSAGE] => Array
                                (
                                    [CODE] => 01
                                    [DESCRIPTION] => On Time
                                )

                            [PACKAGEWEIGHT] => Array
                                (
                                    [UNITOFMEASUREMENT] => Array
                                        (
                                            [CODE] => LBS
                                        )

                                    [WEIGHT] => 3.20
                                )

                            [REFERENCENUMBER] => Array
                                (
                                    [CODE] => 19
                                    [VALUE] => MMTD71EUY061A
                                )

                        )

                )

        )

)

1 个答案:

答案 0 :(得分:1)

如果你已经有数组格式的输出,那么你就没有更多的与json解码有关。

您可以使用$ someArray从数组中获取任何数据。 如果您只想显示或保存它,例如在数据库中,它取决于您。

你想要完成什么?