Thursday 22 April 2021

php: Access elements in a multi-dimensional array

Syntax

$array_name[key1][key2]…[keyN]

 

Example

$krishna_age = $emps['Krishna']['age'];

 

multi_dimension_array_acceess_value.php

#!/usr/bin/php

<?php
$emps = [
    'Chamu' => array(
        'id' => 1,
        'age' => 32,
        'male' => false
    ),
    'Krishna' => array(
        'id' => 2,
        'age' => 32,
        'male' => true
    )
];

$krishna_age = $emps['Krishna']['age'];
$chamu_id = $emps['Chamu']['id'];

echo "Krishna age : $krishna_age\n";
echo "Chamu id : $chamu_id";

?>

 

Output

$./multi_dimension_array_acceess_value.php 

Krishna age : 32
Chamu id : 1

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment