访问PHP数组元素

时间:2014-01-06 18:30:03

标签: php arrays

当我使用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,Year,Make,Model,Trim,Mileage和OptionalEquipment,并将它们作为1个单一参数传递给另一个方法。

它解决了:

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

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

4 个答案:

答案 0 :(得分:1)

无论你走多远,它们都只是数组元素,所以你可以像下面的名字一样引用它们:

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

答案 1 :(得分: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'];

答案 2 :(得分:1)

为简单起见,我假设这个数组保存为$ array。

您可以像这样访问数组中的数据:

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

但是你说你想把它们全部作为一个参数传递,所以要做到这一点你可能只想传递一个数组。

someFuntion($array['GetVehicleConfigurationByVehicleIdResult']);

答案 3 :(得分:1)

你是对的。这里有数组内的数组。并且var_dump非常好地展示了它,因此您可以完全知道如何导航这个多维数组的级别。

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

对于年份,您需要获得$array['GetVehicleConfigurationByVehicleIdResult']['Year']['Value']

我想你现在可以猜到其他人了。