Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Accessing Multidimensional Array Values

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
                        )
                )
       )
)
like image 952
Paul Pavlou Avatar asked Jun 17 '13 01:06

Paul Pavlou


People also ask

What is multidimensional array in PHP explain it with simple PHP code?

A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

How can we get single value from this multi-dimensional PHP array?

We can get the single value from the multi-dimensional array using the index value and the array key as well.

How define multi-dimensional array in PHP?

PHP allows a very simple way to declare a multidimensional array in PHP using the keyword 'array'. In order to declare an array inside another array, We need to add the keyword 'array' and then the elements of that array.


1 Answers

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"];
    }
}
like image 91
Orangepill Avatar answered Sep 22 '22 21:09

Orangepill