PHP - 访问多维数组值 [英] PHP - Accessing Multidimensional Array Values

查看:148
本文介绍了PHP - 访问多维数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时搞乱,出汗和拉我的头发后,我仍然无法访问这些值。我想通过数组的第一级循环,这是一个基本的的foreach循环很简单,但我似乎无法得到的'['适合']'第二子阵列阵列。我环顾四周,但似乎无法得到比真正的基本阵列教程其他任何事情似乎并没有深入到遥远循环。

我想访问嵌套/子阵,即'['Species_name']'中的值。

我不想使用关联键作为排序是有点问题的。

 阵列

    [0] =>排列
        (
            [ID] => 1
            [名] => Bradeley馆游泳池
            [交code => CW1 5QN
            [纬度] => 53.10213
            [伦敦] => -2.41069
            [大小] = GT; 1.60
            [钉] => 21
            [距离] => 26.6
        )    [1] =>排列
        (
            [ID] => 2
            [名] =>农场泳池
            [交code => CW9 6JQ
            [纬度] => 53.320502
            [伦敦] => -2.549049
            [大小] = GT; 0.88
            [钉] => 8
            [距离] => 15.4
            [适宜] =​​>排列
                (
                    [0] =>排列
                        (
                            [fk_water_id] => 2
                            [fk_species_id] => 4
                            [species_name] =>触须
                            [species_rating] => 1
                            [RECORD_ID] => 1
                            [weight_kg] => 2.721554
                            [length_cm] => 40
                            [height_cm] =>三十
                        )
                )
       )


解决方案

这很可能绊倒您的事情是,适用性阵列不仅仅是一个数组的数组,以便在一个例子,你想要得到的species_name财产第一第二顶层元素你会使用类似

  $阵列[1] [适用性] [0] [species_name];

值得注意的是你的第一个数组不包含一个适合的价值,这样将无法被访问。在fo​​reach循环中,你可以使用类似这样的结构:

 的foreach($数组作为$值){
     如果(使用isset($值[适用性]){
            回声$价值[适用性] [0] [species_name];
     }
}

After several hours of messing, sweating and pulling my hair out I'm still unable to access these values. I want to loop through the first level of arrays, and that's simple enough with a basic 'foreach' loop but I can't seem to get to the '['suitability']' array on the second sub array. I've looked around but can't seem to get anything other than really basic array tutorials which don't seem to delve to far into looping.

I'm trying to access the values in the nested/sub array ie '['Species_name']'.

I don't want to use associative keys as the sorting is a bit of an issue.

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Bradeley Hall Pool
            [postcode] => CW1 5QN
            [lat] => 53.10213
            [lon] => -2.41069
            [size] => 1.60
            [pegs] => 21
            [distance] => 26.6
        )

    [1] => Array
        (
            [id] => 2
            [name] => Farm Pool
            [postcode] => CW9 6JQ
            [lat] => 53.320502
            [lon] => -2.549049
            [size] => 0.88
            [pegs] => 8
            [distance] => 15.4
            [suitability] => Array
                (
                    [0] => Array
                        (
                            [fk_water_id] => 2
                            [fk_species_id] => 4
                            [species_name] => Barbel
                            [species_rating] => 1
                            [record_id] => 1
                            [weight_kg] => 2.721554
                            [length_cm] => 40
                            [height_cm] => 30
                        )
                )
       )
)

解决方案

The thing that is probably tripping you up is that suitability is an array of arrays not just an array so in an example where you want to get the species_name property of the first second top level element you would use something like

$array[1]["suitability"][0]["species_name"];

It's worth noting that your first array does not contain a "suitability" value so that would not be able to be accessed. In a foreach loop you could use a construct similar to this:

foreach($array as $value){
     if (isset($value["suitability"]){
            echo $value["suitability"][0]["species_name"];
     }
}

这篇关于PHP - 访问多维数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆