Web Development & WordPress

PHP: Multidimensional Array and to show its value

Code:

<?php
$students=[
    '12'=>'Tom',
    '13'=>'Shib',
    '14'=>[
        '12'=>'Tom Spanish',
        '13'=>'Shib Spanish'
    ]
];
var_dump($students);
echo "<br>";

print_r($students);
echo "<br>";

echo $students["14"]["13"];
echo "<br>";
$food=array(
    "vegetables" => "tomato,potato",
    "frutis" => "apple, banana",
    "drinks" => array(
        "soft"=>"coke,fanta",
        "beverage" => "x,y,z",
    )
);

print_r($food);
echo "<br>";
echo $food["drinks"]["soft"];
?>

Output

array(3) { [12]=> string(3) "Tom" [13]=> string(4) "Shib" [14]=> array(2) { [12]=> string(11) "Tom Spanish" [13]=> string(12) "Shib Spanish" } }
Array ( [12] => Tom [13] => Shib [14] => Array ( [12] => Tom Spanish [13] => Shib Spanish ) )
Shib Spanish
Array ( [vegetables] => tomato,potato [frutis] => apple, banana [drinks] => Array ( [soft] => coke,fanta [beverage] => x,y,z ) )
coke,fanta

Leave a comment