Question

echo $a['b']['b2'];

What does the value in the brackets refer to? Thanks.

Was it helpful?

Solution

This is an array.

what you are seeing is

    <?php
    $a = array(
        'b' => array(
          'b2' => 'x'
        )
    );

So in this case, $a['b']['b2'] will have a value of 'x'. This is just my example though, there could be more arrays in the tree. Refer to the PHP Manual

OTHER TIPS

Those are keys of a multidimensional array.

It may refer to this array:

$a = array(
    "a" => array(
        "a1" => "foo",
        "a2" => "bar"
    ),
    "b" => array(
        "b1" => "baz",
        "b2" => "bin"
    )
)

In this case, $a['b']['b2'] would refer to 'bin'

This refers to a two dimensional array, and the value inside the bracket shows the key of the array

That means the variable $a holds an array. The values inside of the brackets refer the array keys.

$a = array('b' => 'somevalue', 'b2' => 'somevalue2');

In this case echo'ing $a['b'] would output it's value of 'somevalue' and $a['b2'] would output it's value of 'somevalue2'.

In your example, it's refering to a multi-dimensional array (an array inside of an array)

$a = array('b' => array('b2' => 'b2 value'));

where calling b2 would output 'b2 value'

Sorry if my answer is too simplistic, not sure your level of knowledge :)

$a is an array, a list of items. Most programming languages allow you to access items in the array using a number, but PHP also allows you to access them by a string, like 'b' or 'b2'.

Additionally, you have a two-dimensional array there - an array of arrays. So in that example, you are printing out the 'b2' element of the 'b' element in the $a array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top