Saturday, 28 July 2012

Accessing array elements with square bracket syntax

Array elements can be accessed using the array[key] syntax.
Example #7 Accessing array elements
<?php
$array 
= array(
    
"foo" => "bar",
    
42    => 24,
    
"multi" => array(
         
"dimensional" => array(
             
"array" => "foo"
         
)
    )
);
var_dump($array["foo"]);var_dump($array[42]);var_dump($array["multi"]["dimensional"]["array"]);?>
The above example will output:
string(3) "bar"
int(24)
string(3) "foo"
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

No comments:

Post a Comment