PHP多维数组搜索键和显示值

时间:2012-02-01 09:37:49

标签: php multidimensional-array

下面是数组的结果,我只想从中获取坐标并将它们存储到一维数组中。

  Array
(
  [name] => jackson
  [Status] => Array
    (
      [code] => 200
      [request] => geocode
    )

  [Placemark] => Array
    (
      [0] => Array
        (
          [@attributes] => Array
            (
              [id] => p1
            )

          [address] => Jackson, MS, USA
          [AddressDetails] => Array
            (
              [@attributes] => Array
                (
                  [Accuracy] => 4
                )

              [Country] => Array
                (
                  [CountryNameCode] => US
                  [CountryName] => USA
                  [AdministrativeArea] => Array
                    (
                      [AdministrativeAreaName] => MS
                      [SubAdministrativeArea] => Array
                        (
                          [SubAdministrativeAreaName] => Hinds
                          [Locality] => Array
                            (
                              [LocalityName] => Jackson
                            )

                        )

                    )

                )

            )

          [ExtendedData] => Array
            (
              [LatLonBox] => Array
                (
                  [@attributes] => Array
                    (
                      [north] => 32.3741783
                      [south] => 32.2232735
                      [east] => -90.0567509
                      [west] => -90.3128697
                    )

                )

            )

          [Point] => Array
            (
              [coordinates] => -90.1848103,32.2987573,0
            )

        )

      [1] => Array
        (
          [@attributes] => Array
            (
              [id] => p2
            )

          [address] => Jackson, TN, USA
          [AddressDetails] => Array
            (
              [@attributes] => Array
                (
                  [Accuracy] => 4
                )

              [Country] => Array
                (
                  [CountryNameCode] => US
                  [CountryName] => USA
                  [AdministrativeArea] => Array
                    (
                      [AdministrativeAreaName] => TN
                      [SubAdministrativeArea] => Array
                        (
                          [SubAdministrativeAreaName] => Madison
                          [Locality] => Array
                            (
                              [LocalityName] => Jackson
                            )

                        )

                    )

                )

            )

          [ExtendedData] => Array
            (
              [LatLonBox] => Array
                (
                  [@attributes] => Array
                    (
                      [north] => 35.7562880
                      [south] => 35.5402259
                      [east] => -88.7567579
                      [west] => -88.9204599
                    )

                )

            )

          [Point] => Array
            (
              [coordinates] => -88.8139469,35.6145169,0
            )

        )

      [2] => Array
        (
          [@attributes] => Array
            (
              [id] => p3
            )

          [address] => Jackson, WY, USA
          [AddressDetails] => Array
            (
              [@attributes] => Array
                (
                  [Accuracy] => 4
                )

              [Country] => Array
                (
                  [CountryNameCode] => US
                  [CountryName] => USA
                  [AdministrativeArea] => Array
                    (
                      [AdministrativeAreaName] => WY
                      [SubAdministrativeArea] => Array
                        (
                          [SubAdministrativeAreaName] => Teton
                          [Locality] => Array
                            (
                              [LocalityName] => Jackson
                            )

                        )

                    )

                )

            )

          [ExtendedData] => Array
            (
              [LatLonBox] => Array
                (
                  [@attributes] => Array
                    (
                      [north] => 43.4912050
                      [south] => 43.4578330
                      [east] => -110.7377220
                      [west] => -110.8134730
                    )

                )

            )

          [Point] => Array
            (
              [coordinates] => -110.7624282,43.4799291,0
            )

        )

      [3] => Array
        (
          [@attributes] => Array
            (
              [id] => p4
            )

          [address] => Jackson, NJ, USA
          [AddressDetails] => Array
            (
              [@attributes] => Array
                (
                  [Accuracy] => 4
                )

              [Country] => Array
                (
                  [CountryNameCode] => US
                  [CountryName] => USA
                  [AdministrativeArea] => Array
                    (
                      [AdministrativeAreaName] => NJ
                      [SubAdministrativeArea] => Array
                        (
                          [SubAdministrativeAreaName] => Ocean
                          [Locality] => Array
                            (
                              [LocalityName] => Jackson
                            )

                        )

                    )

                )

            )

          [ExtendedData] => Array
            (
              [LatLonBox] => Array
                (
                  [@attributes] => Array
                    (
                      [north] => 40.1723549
                      [south] => 39.9990330
                      [east] => -74.2415390
                      [west] => -74.4695430
                    )

                )

            )

          [Point] => Array
            (
              [coordinates] => -74.3294444,40.1080556,0
            )

        )

      [4] => Array
        (
          [@attributes] => Array
            (
              [id] => p5
            )

          [address] => Jackson, MI, USA
          [AddressDetails] => Array
            (
              [@attributes] => Array
                (
                  [Accuracy] => 4
                )

              [Country] => Array
                (
                  [CountryNameCode] => US
                  [CountryName] => USA
                  [AdministrativeArea] => Array
                    (
                      [AdministrativeAreaName] => MI
                      [SubAdministrativeArea] => Array
                        (
                          [SubAdministrativeAreaName] => Jackson
                          [Locality] => Array
                            (
                              [LocalityName] => Jackson
                            )

                        )

                    )

                )

            )

          [ExtendedData] => Array
            (
              [LatLonBox] => Array
                (
                  [@attributes] => Array
                    (
                      [north] => 42.2708699
                      [south] => 42.2036839
                      [east] => -84.3568169
                      [west] => -84.4345460
                    )

                )

            )

          [Point] => Array
            (
              [coordinates] => -84.4013462,42.2458690,0
            )

        )

      [5] => Array
        (
          [@attributes] => Array
            (
              [id] => p6
            )

          [address] => Jackson, WI, USA
          [AddressDetails] => Array
            (
              [@attributes] => Array
                (
                  [Accuracy] => 4
                )

              [Country] => Array
                (
                  [CountryNameCode] => US
                  [CountryName] => USA
                  [AdministrativeArea] => Array
                    (
                      [AdministrativeAreaName] => WI
                      [SubAdministrativeArea] => Array
                        (
                          [SubAdministrativeAreaName] => Washington
                          [Locality] => Array
                            (
                              [LocalityName] => Jackson
                            )

                        )

                    )

                )

            )

          [ExtendedData] => Array
            (
              [LatLonBox] => Array
                (
                  [@attributes] => Array
                    (
                      [north] => 43.3387369
                      [south] => 43.3090429
                      [east] => -88.1422039
                      [west] => -88.1917450
                    )

                )

            )

          [Point] => Array
            (
              [coordinates] => -88.1667599,43.3238919,0
            )

        )

      [6] => Array
        (
          [@attributes] => Array
            (
              [id] => p7
            )

          [address] => Jackson, CA, USA
          [AddressDetails] => Array
            (
              [@attributes] => Array
                (
                  [Accuracy] => 4
                )

              [Country] => Array
                (
                  [CountryNameCode] => US
                  [CountryName] => USA
                  [AdministrativeArea] => Array
                    (
                      [AdministrativeAreaName] => CA
                      [SubAdministrativeArea] => Array
                        (
                          [SubAdministrativeAreaName] => Amador
                          [Locality] => Array
                            (
                              [LocalityName] => Jackson
                            )

                        )

                    )

                )

            )

          [ExtendedData] => Array
            (
              [LatLonBox] => Array
                (
                  [@attributes] => Array
                    (
                      [north] => 38.3721550
                      [south] => 38.3302920
                      [east] => -120.7489930
                      [west] => -120.7981980
                    )

                )

            )

          [Point] => Array
            (
              [coordinates] => -120.7741018,38.3488023,0
            )

        )

      [7] => Array
        (
          [@attributes] => Array
            (
              [id] => p8
            )

          [address] => Jackson, GA, USA
          [AddressDetails] => Array
            (
              [@attributes] => Array
                (
                  [Accuracy] => 4
                )

              [Country] => Array
                (
                  [CountryNameCode] => US
                  [CountryName] => USA
                  [AdministrativeArea] => Array
                    (
                      [AdministrativeAreaName] => GA
                      [SubAdministrativeArea] => Array
                        (
                          [SubAdministrativeAreaName] => Butts
                          [Locality] => Array
                            (
                              [LocalityName] => Jackson
                            )

                        )

                    )

                )

            )

          [ExtendedData] => Array
            (
              [LatLonBox] => Array
                (
                  [@attributes] => Array
                    (
                      [north] => 33.3114890
                      [south] => 33.2638330
                      [east] => -83.9354860
                      [west] => -84.0105969
                    )

                )

            )

          [Point] => Array
            (
              [coordinates] => -83.9660209,33.2945651,0
            )

        )

      [8] => Array
        (
          [@attributes] => Array
            (
              [id] => p9
            )

          [address] => Jackson, OH 45640, USA
          [AddressDetails] => Array
            (
              [@attributes] => Array
                (
                  [Accuracy] => 4
                )

              [Country] => Array
                (
                  [CountryNameCode] => US
                  [CountryName] => USA
                  [AdministrativeArea] => Array
                    (
                      [AdministrativeAreaName] => OH
                      [SubAdministrativeArea] => Array
                        (
                          [SubAdministrativeAreaName] => Jackson
                          [Locality] => Array
                            (
                              [LocalityName] => Jackson
                            )

                        )

                    )

                )

            )

          [ExtendedData] => Array
            (
              [LatLonBox] => Array
                (
                  [@attributes] => Array
                    (
                      [north] => 39.0669040
                      [south] => 39.0062730
                      [east] => -82.5949490
                      [west] => -82.7069350
                    )

                )

            )

          [Point] => Array
            (
              [coordinates] => -82.6365536,39.0520169,0
            )

        )

      [9] => Array
        (
          [@attributes] => Array
            (
              [id] => p10
            )

          [address] => Jackson, MO, USA
          [AddressDetails] => Array
            (
              [@attributes] => Array
                (
                  [Accuracy] => 4
                )

              [Country] => Array
                (
                  [CountryNameCode] => US
                  [CountryName] => USA
                  [AdministrativeArea] => Array
                    (
                      [AdministrativeAreaName] => MO
                      [SubAdministrativeArea] => Array
                        (
                          [SubAdministrativeAreaName] => Cape Girardeau
                          [Locality] => Array
                            (
                              [LocalityName] => Jackson
                            )

                        )

                    )

                )

            )

          [ExtendedData] => Array
            (
              [LatLonBox] => Array
                (
                  [@attributes] => Array
                    (
                      [north] => 37.4139659
                      [south] => 37.3385089
                      [east] => -89.5972280
                      [west] => -89.7035499
                    )

                )

            )

          [Point] => Array
            (
              [coordinates] => -89.6662063,37.3822732,0
            )

        )

    )
)

//单个结果的数据从这里开始

  Array
(
    [Response] => Array
        (
            [name] => 10121
            [Status] => Array
                (
                    [code] => 200
                    [request] => geocode
                )

            [Placemark] => Array
                (
                    [@attributes] => Array
                        (
                            [id] => p1
                        )

                    [address] => Manhattan, NY 10121, USA
                    [AddressDetails] => Array
                        (
                            [@attributes] => Array
                                (
                                    [Accuracy] => 5
                                )

                            [Country] => Array
                                (
                                    [CountryNameCode] => US
                                    [CountryName] => USA
                                    [AdministrativeArea] => Array
                                        (
                                            [AdministrativeAreaName] => NY
                                            [DependentLocality] => Array
                                                (
                                                    [DependentLocalityName] => Manhattan
                                                    [PostalCode] => Array
                                                        (
                                                            [PostalCodeNumber] => 10121
                                                        )

                                                )

                                        )

                                )

                        )

                    [ExtendedData] => Array
                        (
                            [LatLonBox] => Array
                                (
                                    [@attributes] => Array
                                        (
                                            [north] => 40.7528519
                                            [south] => 40.7489381
                                            [east] => -73.9917906
                                            [west] => -73.9947563
                                        )

                                )

                        )

                    [Point] => Array
                        (
                            [coordinates] => -73.9917906,40.7492821,0
                        )

                )

        )

)

//单个结果的数据在此处结束

我尝试了以下代码以获得所需的结果,但没有成功

foreach ($array as $xm) {
        foreach ($xm as $points=>$pointkey) {
            foreach($pointkey as $cor=>$corkey) {
                echo $cor["coordinates"];
            }
        }
}

它给了我警告“警告:在foreach中为foreach()提供的参数无效($ pointkey为$ cor => $ corkey)”

3 个答案:

答案 0 :(得分:1)

尝试类似:

$coords = array();
foreach ($data['Placemark'] as $entry) { // where $data holds the complete array
  $coords[] = $entry['Point']['coordinates'];
}

var_dump($coords);

只有一个结果,您可以直接访问坐标,例如:

var_dump($data['Response']['Placemark']['Point']['coordinates']);

答案 1 :(得分:0)

我会做这样的事情:

$coords = array();
array_walk_recursive($input_array, 'get_coords');

function get_coords($item, $key)
{
    $coord = ($key === 'coordinates') ? $item : '';
    if(!empty($coord))
    {
        $coords[] = $coord;
    }
}

答案 2 :(得分:0)

Array
(
    [0] => Array
        (
            [@attributes] => Array
                (
                    [request_id] => 0
                    [district] => 
                    [county] => WILTS
                    [ptc_abs_code] => 58150004231
                    [house_no] => 232
                    [post_town] => WESTBURY
                    [match_status] => 1
                    [house_name] => 
                    [postcode] => BA133BN
                    [surname] => 
                    [street_2] => 
                    [street_1] => HIGH ST
                )

        )

)