2012-02-01 84 views
-2

下面是從我想要抓住僅座標,並將它們存儲在所述一個維陣列的陣列的結果。PHP多維數組搜索鍵和顯示值

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

那麼你真的嘗試過自己?還是你只是希望有人會爲你做你的工作? – 2012-02-01 09:38:54

+0

另外我沒有看到一個真正的問題,*座標*都位於相同的位置,只是寫一個foreach循環,並收集值? – Yoshi 2012-02-01 09:44:53

+0

@Yoshi你能告訴我什麼是我的foreach循環? – dave 2012-02-01 09:45:52

回答

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']); 
+0

謝謝@Yoshi,你的代碼很簡單。和工作。 – dave 2012-02-01 09:54:08

+0

當有一個結果,它給人的錯誤( 致命錯誤:無法使用字符串數組offset)爲$ COORDS [] – dave 2012-02-01 10:17:48

+0

@戴夫你能包括與結果只有一個數據? – Yoshi 2012-02-01 10:20:47

0

我會做這樣的事情:

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

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

即使輸入數據的結構發生變化,這也可以工作。與Yoshi的不同,它將省略沒有座標值的任何鍵。 – deed02392 2012-02-01 09:53:16

+0

謝謝@ deed02392。但它給在array_walk_recursive警告(功能沒有找到get_coords)請告訴我爲什麼它再次提出警告 – dave 2012-02-01 09:55:02

+0

更改結構,在這種類型的數據,也可能意味着修改了它的意思。所以我不認爲盲目搜索*座標*鍵是個好主意。 – Yoshi 2012-02-01 09:59:02

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 
       ) 

     ) 

)