PHP Access Element in Multidimensional Array
In this chapter you will learn:
- How to access element in multidimensional array
- Syntax to access element in multidimensional array
- Example - Access elements in multi-dimensional array
Description
Square bracket syntax can access any element within a multidimensional array.
Syntax
To access the first dimension
$multidimensionalArray[]
To access the inner dimension
$multidimensionalArray[][]
Example
Here are some examples
<?php /*from j a v a 2 s. c o m*/
$myBooks = array(
array(
"title" => "Learn PHP from java2s.com",
"author" => "java2s.com",
"pubYear" => 2000
),
array(
"title" => "Learn Java from java2s.com",
"author" => "JavaAuthor",
"pubYear" => 2001
),
array(
"title" => "Learn HTML from java2s.com",
"author" => "HTMLAuthor",
"pubYear" => 2002
),
array(
"title" => "Learn CSS from java2s.com",
"author" => "CSSAuthor",
"pubYear" => 2003
),
);
print_r( $myBooks[1] );
echo $myBooks[1]["title"] . "\n";
echo $myBooks[3]["pubYear"] . "\n";
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- How to Loop Through Multidimensional Array
- Syntax to Loop Through Multidimensional Array
- Example - Loop Through Multidimensional Array
Home » PHP Tutorial » PHP Array