Page 155 - Beginning PHP 5.3
P. 155
Chapter 6: Arrays
This ability of arrays to store other arrays in their elements allows you to create multidimensional arrays
(also known as nested arrays because they comprise one or more arrays nested inside another). 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.
Creating a Multidimensional Array
The following script creates a simple two - dimensional array called $myBooks , then displays its contents
using print_r() . You can see the result in Figure 6-4 .
< !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
< html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
< head >
< title > A Two-Dimensional Array < /title >
< link rel=”stylesheet” type=”text/css” href=”common.css” / >
< /head >
< body >
< h1 > A Two-Dimensional Array < /h1 >
< ?php
$myBooks = array(
array(
“title” = > “The Grapes of Wrath”,
“author” = > “John Steinbeck”,
“pubYear” = > 1939
),
array(
“title” = > “The Trial”,
“author” = > “Franz Kafka”,
“pubYear” = > 1925
),
array(
“title” = > “The Hobbit”,
“author” = > “J. R. R. Tolkien”,
“pubYear” = > 1937
),
array(
“title” = > “A Tale of Two Cities”,
“author” = > “Charles Dickens”,
“pubYear” = > 1859
),
);
echo “ < pre > ”;
print_r ( $myBooks );
echo “ < /pre > ”;
? >
< /body >
< /html >
117
9/21/09 9:00:15 AM
c06.indd 117
c06.indd 117 9/21/09 9:00:15 AM