-1

Below is the multidimensional array with define function. How would I echo so that the output value would show 5 from array 'a' => array 'apple'?

I tried echo Hello[0][2], but it doesn't show anything in the output.

define('Hello',
  array(
    'a' => array(
      'apple' => array(1,3,5),
      'orange' => array(543,345,345345),
      'grapes' => array(64,576,353)
      ),
    'b' => array(
      'apple' => array(1,3,5),
      'orange' => array(523,342,34645),
      'grapes' => array(66,76,33)
      )
    )
);

2 Answers2

1

I think are accessing by wrong indexes as print_r(Hello); shows


Array
(
    [a] => Array
        (
            [apple] => Array
                (
                    [0] => 1
                    [1] => 3
                    [2] => 5
                )

            [orange] => Array
                (
                    [0] => 543
                    [1] => 345
                    [2] => 345345
                )

            [grapes] => Array
                (
                    [0] => 64
                    [1] => 576
                    [2] => 353
                )

        )

    [b] => Array
        (
            [apple] => Array
                (
                    [0] => 1
                    [1] => 3
                    [2] => 5
                )

            [orange] => Array
                (
                    [0] => 523
                    [1] => 342
                    [2] => 34645
                )

            [grapes] => Array
                (
                    [0] => 66
                    [1] => 76
                    [2] => 33
                )

        )

)

so you need to access

Hello['a']['apple'][0];
Hello['b']['orange'][2];

which will give you 1 and 34645

Ronak Dhoot
  • 2,322
  • 1
  • 12
  • 19
0

Just by using this:

echo Hello['a']['apple'][2];
Ankur Mishra
  • 1,284
  • 1
  • 6
  • 15