多维数组PHP输出值作为一行

时间:2014-09-14 16:07:35

标签: php arrays multidimensional-array output

这是我的数组

        Array
(
    [TrackResponse] => Array
        (
            [Response] => Array
                (
                    [TransactionReference] => Array
                        (
                            [CustomerContext] => RocketShipIt
                        )

                    [ResponseStatusCode] => 1
                    [ResponseStatusDescription] => Success
                )

            [Shipment] => Array
                (
                    [Shipper] => Array
                        (
                            [ShipperNumber] => 746354
                            [Address] => Array
                                (
                                    [AddressLine1] => 11 PINE ST
                                    [City] => NEW BEDFORD
                                    [StateProvinceCode] => MA
                                    [PostalCode] => 01032   9785
                                    [CountryCode] => US
                                )

                        )

                    [ShipTo] => Array
                        (
                            [Address] => Array
                                (
                                    [City] => FOUNTAIN HILLS
                                    [StateProvinceCode] => AZ
                                    [PostalCode] => 85268
                                    [CountryCode] => US
                                )

                        )

                    [ShipmentWeight] => Array
                        (
                            [UnitOfMeasurement] => Array
                                (
                                    [Code] => LBS
                                )

                            [Weight] => 1.00
                        )

                    [Service] => Array
                        (
                            [Code] => 003
                            [Description] => GROUND
                        )

                    [ShipmentIdentificationNumber] => 746463772264354327
                    [PickupDate] => 20140709
                    [DeliveryDateUnavailable] => Array
                        (
                            [Type] => Scheduled Delivery
                            [Description] => Scheduled Delivery Date is not currently available, please try back later
                        )

                    [Package] => Array
                        (
                            [TrackingNumber] => 746463772264354327
                            [Activity] => Array
                                (
                                    [0] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [City] => FOUNTAIN HILLS
                                                            [StateProvinceCode] => AZ
                                                            [PostalCode] => 85268
                                                            [CountryCode] => US
                                                        )

                                                    [Code] => ML
                                                    [Description] => FRONT DOOR
                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => D
                                                            [Description] => DELIVERED
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => FS
                                                        )

                                                )

                                            [Date] => 20140716
                                            [Time] => 142400
                                        )

                                    [1] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [City] => TEMPE
                                                            [StateProvinceCode] => AZ
                                                            [CountryCode] => US
                                                        )

                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => I
                                                            [Description] => OUT FOR DELIVERY
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => DS
                                                        )

                                                )

                                            [Date] => 20140716
                                            [Time] => 041900
                                        )

                                    [2] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [City] => TEMPE
                                                            [StateProvinceCode] => AZ
                                                            [CountryCode] => US
                                                        )

                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => I
                                                            [Description] => ARRIVAL SCAN
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => AR
                                                        )

                                                )

                                            [Date] => 20140715
                                            [Time] => 114500
                                        )

                                    [3] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [City] => HODGKINS
                                                            [StateProvinceCode] => IL
                                                            [CountryCode] => US
                                                        )

                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => I
                                                            [Description] => DEPARTURE SCAN
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => DP
                                                        )

                                                )

                                            [Date] => 20140711
                                            [Time] => 144600
                                        )

                                    [4] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [City] => HODGKINS
                                                            [StateProvinceCode] => IL
                                                            [CountryCode] => US
                                                        )

                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => I
                                                            [Description] => LOCATION SCAN
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => LC
                                                        )

                                                )

                                            [Date] => 20140711
                                            [Time] => 090500
                                        )

                                    [5] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [City] => HODGKINS
                                                            [StateProvinceCode] => IL
                                                            [CountryCode] => US
                                                        )

                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => I
                                                            [Description] => UNLOAD SCAN
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => UL
                                                        )

                                                )

                                            [Date] => 20140711
                                            [Time] => 085300
                                        )

                                    [6] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [City] => HODGKINS
                                                            [StateProvinceCode] => IL
                                                            [CountryCode] => US
                                                        )

                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => I
                                                            [Description] => ARRIVAL SCAN
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => AR
                                                        )

                                                )

                                            [Date] => 20140711
                                            [Time] => 061900
                                        )

                                    [7] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [City] => W SPRINGFIELD
                                                            [StateProvinceCode] => MA
                                                            [CountryCode] => US
                                                        )

                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => I
                                                            [Description] => DEPARTURE SCAN
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => DP
                                                        )

                                                )

                                            [Date] => 20140709
                                            [Time] => 213400
                                        )

                                    [8] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [City] => W SPRINGFIELD
                                                            [StateProvinceCode] => MA
                                                            [CountryCode] => US
                                                        )

                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => I
                                                            [Description] => ORIGIN SCAN
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => OR
                                                        )

                                                )

                                            [Date] => 20140709
                                            [Time] => 183400
                                        )

                                    [9] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [City] => W SPRINGFIELD
                                                            [StateProvinceCode] => MA
                                                            [CountryCode] => US
                                                        )

                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => P
                                                            [Description] => PICKUP SCAN
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => PU
                                                        )

                                                )

                                            [Date] => 20140709
                                            [Time] => 163200
                                        )

                                    [10] => Array
                                        (
                                            [ActivityLocation] => Array
                                                (
                                                    [Address] => Array
                                                        (
                                                            [CountryCode] => US
                                                        )

                                                )

                                            [Status] => Array
                                                (
                                                    [StatusType] => Array
                                                        (
                                                            [Code] => M
                                                            [Description] => BILLING INFORMATION RECEIVED
                                                        )

                                                    [StatusCode] => Array
                                                        (
                                                            [Code] => MP
                                                        )

                                                )

                                            [Date] => 20140709
                                            [Time] => 112842
                                        )

                                )

                            [PackageWeight] => Array
                                (
                                    [UnitOfMeasurement] => Array
                                        (
                                            [Code] => LBS
                                        )

                                    [Weight] => 1.00
                                )

                        )

                )

        )

)

我想要访问 Activity 数字索引数组中的值。



    foreach($response as $row)
    {
      foreach($row['Shipment']['Package'] as $k)
         {
             echo $k['ActivityLocation']['Address']['City'];
         }
    } 

我希望能够访问数字键控数组中的值,然后将它们打印成1-10行等。

我得到的错误是致命错误:不能在第287行的test.php中使用字符串偏移作为数组

如果有人能帮助我解决这个问题,那么你将是最好的! 请和谢谢

1 个答案:

答案 0 :(得分:0)

  foreach($row['Shipment']['Package'] as $k)
   {
       echo $k['ActivityLocation']['Address']['City'];
   }