2014-01-06 241 views
-3

我有一個PHP數組,當我用var_dump()這是結果我得到:訪問PHP數組元素

array(1) { ["GetVehicleConfigurationByVehicleIdResult"]=> array(9) { ["Id"]=> string(1) "2" ["VIN"]=> NULL ["Year"]=> array(2) { ["Id"]=> string(4) "2006" ["Value"]=> string(4) "2006" } ["Make"]=> array(2) { ["Id"]=> string(1) "2" ["Value"]=> string(5) "Acura" } ["Model"]=> array(2) { ["Id"]=> string(1) "2" ["Value"]=> string(2) "TL" } ["Trim"]=> array(2) { ["Id"]=> string(6) "268650" ["Value"]=> string(12) "3.2 Sedan 4D" } ["Mileage"]=> string(6) "100000" ["OptionalEquipment"]=> array(1) { ["EquipmentOption"]=> array(35) { [0]=> array(13) { ["DisplayName"]=> string(19) "V6, VTEC, 3.2 Liter" ["VehicleOptionId"]=> string(3) "204" ["IsSelected"]=> string(4) "true" ["OptionTypeDisplayName"]=> string(6) "Engine" ["OptionGroupName"]=> string(3) "N/A" ["DisplayNameAdditionalData"]=> string(3) "N/A" ["ManufactureCode"]=> string(0) "" ["OptionAvailabilityDisplayName"]=> string(3) "N/A" ["IsDefaultConfiguration"]=> string(4) "true" ["DetailName"]=> string(3) "N/A" ["NonBoldName"]=> string(3) "N/A" ["Footer"]=> string(3) "N/A" ["SortOrder"]=> string(4) "1000" } 

如何,我可以得到從這個數組中的元素? 一些元素很複雜,就像它們是數組內部的數組一樣。

這是格式化打印出數組中更好地理解:

Array 
(
[GetVehicleConfigurationByVehicleIdResult] => Array 
    (
     [Id] => 2 
     [VIN] => 
     [Year] => Array 
      (
       [Id] => 2006 
       [Value] => 2006 
      ) 

     [Make] => Array 
      (
       [Id] => 2 
       [Value] => Acura 
      ) 

     [Model] => Array 
      (
       [Id] => 2 
       [Value] => TL 
      ) 

     [Trim] => Array 
      (
       [Id] => 268650 
       [Value] => 3.2 Sedan 4D 
      ) 

     [Mileage] => 100000 
     [OptionalEquipment] => Array 
      (
       [EquipmentOption] => Array 
        (
         [0] => Array 
          (
           [DisplayName] => V6, VTEC, 3.2 Liter 
           [VehicleOptionId] => 204 
           [IsSelected] => true 
           [OptionTypeDisplayName] => Engine 
           [OptionGroupName] => N/A 
           [DisplayNameAdditionalData] => N/A 
           [ManufactureCode] => 
           [OptionAvailabilityDisplayName] => N/A 
           [IsDefaultConfiguration] => true 
           [DetailName] => N/A 
           [NonBoldName] => N/A 
           [Footer] => N/A 
           [SortOrder] => 1000 
          ) 

我想:ID,VIN,年份,牌子,型號,裝飾條,里程和OptionalEquipment,並通過他們作爲1單參數指向另一個方法。

它解決:

$Id = $resultVehicleId['GetVehicleConfigurationByVehicleIdResult']['Id']; 

$Year = $resultVehicleId['GetVehicleConfigurationByVehicleIdResult']['Year']['Value']; 
+0

您的谷歌搜索詞是[*「多維」*](https://www.google.com/search?q=php+multi+dimensional+array&oq=php+multi+dimensional+array)。 – Emissary

+0

id = $ array ['Model'] ['id']; – user1336827

+0

'$ arrayname ['GetVehicleConfigurationByVehicleIdResult'] ['Id']'例如,或'$ arrayname ['GetVehicleConfigurationByVehicleIdResult'] ['Year'] ['Value']' – kero

回答

1

你是對的。這裏有數組內部的數組。而var_dump顯示它非常好,所以你可以完全知道如何瀏覽這個多維數組的水平。

如果你想VIN只得到$array['GetVehicleConfigurationByVehicleIdResult']['VIN']

對於一年你需要得到$array['GetVehicleConfigurationByVehicleIdResult']['Year']['Value']

我想你現在可以猜測別人。

1

他們只是數組元素不管你有多深,你跑那麼可以只引用他們的名字象下面這樣:

$id = $that_array['GetVehicleConfigurationByVehicleIdResult']['Id']; 
+0

$ that_array是什麼?我在那裏使用的任何東西我有一個錯誤。 – nikta

+0

$ that_array是您在var_dump()中放置的變量。這是一個佔位符名稱。 – user3087663

1
$id = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Id'];; 
$vin = $array_name["GetVehicleConfigurationByVehicleIdResult"]['VIN']; 
$year = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Year'] 
$make = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Make']; 
$model = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Model']; 
$trim = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Trim']['Value']; 
$mileage = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Mileage']; 
$optional_equipment = $array_name["GetVehicleConfigurationByVehicleIdResult"]['OptionalEquipment']['EquipmentOption'][0]['DisplayName']; 
+0

我使用了這一行,但它給了我錯誤:Undefined variable:array_name – nikta

1

爲了簡便起見,我假設這個數組被保存爲$ array。

您可以從陣列訪問數據是這樣的:

$vin = $array['GetVehicleConfigurationByVehicleIdResult']['VIN']; 

但你說,你想他們都通過一個單一的參數,所以這樣做,你可能會想只傳遞一個數組。

someFuntion($array['GetVehicleConfigurationByVehicleIdResult']);