PHP Access Element in Multidimensional Array

In this chapter you will learn:

  1. How to access element in multidimensional array
  2. Syntax to access element in multidimensional array
  3. 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:

  1. How to Loop Through Multidimensional Array
  2. Syntax to Loop Through Multidimensional Array
  3. Example - Loop Through Multidimensional Array
Home » PHP Tutorial » PHP Array
PHP Array
PHP Create Indexed Array
PHP Create Associative Arrays
PHP Array Operator
PHP Access Array Element
PHP Change Array Element
PHP Create Array using Square Bracket
PHP Array Element Loop Through
PHP Array foreach loop
PHP Change Array Values with foreach
PHP Array Multidimensional
PHP Access Element in Multidimensional Array
PHP Loop Through Multidimensional Array