Tags
Asked 3 years ago
9 Jun 2021
Views 305
pratik

pratik posted

Accessing Multidimensional Array Values in php ?

Accessing Multidimensional Array Values in php ?
eclipse-learner

eclipse-learner
answered Apr 26 '23 00:00

In PHP, accessing values of a multidimensional array involves specifying the index/key of each dimension. For example, let's say we have the following multidimensional array:



$fruits = array(
    "apple" => array("color" => "red", "taste" => "sweet"),
    "banana" => array("color" => "yellow", "taste" => "sweet"),
    "orange" => array("color" => "orange", "taste" => "sour")
);

To access the value of a specific element in the array, we use the key/index of each dimension. For example, to access the color of the apple, we would use:



echo $fruits["apple"]["color"]; // Output: red

Similarly, to access the taste of the banana, we would use:



echo $fruits["banana"]["taste"]; // Output: sweet

And to access the color of the orange, we would use:



echo $fruits["orange"]["color"]; // Output: orang
Post Answer