PHP Array Multidimensional

Description

A multi-dimensional array can have array as its value.

An array that contains other arrays is a two-dimensional array. If those arrays also contain arrays, then the top-level array is a three-dimensional array, and so on.

Example

The following code uses array operator ([]) to create an two-dimensional array. The key of the first level is Java and PHP.


<?PHP//from ww w.j ava  2 s  .c  o  m
$book['Java'] = array("Name"=>"Java Book", "Price"=> 1.2, "OnSale"=>"No");
$book['PHP'] = array("Name"=>"PHP Book", "Price"=>5.3, "OnSale"=>"Yes");
var_dump($book);
?>

The code above generates the following result.

Example 2

The following script creates a simple two-dimensional array called $myBooks , then displays its contents using print_r().


<?php //from  w w w.j  a  va  2s.co  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 ); 
?>  

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone