Home » Php » How to echo multidimension array define() function in php?

How to echo multidimension array define() function in php?

Posted by: admin February 25, 2020 Leave a comment

Questions:

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)
      )
    )
);
How to&Answers:

Just by using this:

echo Hello['a']['apple'][2];

Answer:

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