Looping Through Array Keys and Values
- title:
- Rear Window
- director:
- Alfred Hitchcock
- year:
- 1954
- minutes:
- 112
<?php
$movie = array( "title" => "Rear Window",
"director" => "Alfred Hitchcock",
"year" => 1954,
"minutes" => 112 );
echo "<dl>";
foreach ( $movie as $key => $value ) {
echo "<dt>$key:</dt>";
echo "<dd>$value</dd>";
}
echo "</dl>";
?>
Counting Multidimensional Arrays
2
<?php
$movieInfo = array( "directors" => array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" ),
"movies" => array( "Rear Window", "2001", "Taxi Driver", "Metropolis" ) );
// Displays "2"
echo count( $movieInfo );
?>
Accessing Elements in Multidimensional Arrays
six
<?php
$myArray = array(
array( "one", "two", "three" ),
array( "four", "five", "six" )
);
// Displays "six"
echo $myArray[1][2];
?>
Accessing Elements in a Multidimensional Array
The title of the first movie:
Rear Window
The director of the third movie:
Martin Scorsese
The nested array contained in the first element:
Array
(
[title] => Rear Window
[director] => Alfred Hitchcock
[year] => 1954
)
<?php
$movies = array(
array(
"title" => "Rear Window",
"director" => "Alfred Hitchcock",
"year" => 1954
),
array(
"title" => "Full Metal Jacket",
"director" => "Stanley Kubrick",
"year" => 1987
),
array(
"title" => "Mean Streets",
"director" => "Martin Scorsese",
"year" => 1973
)
);
echo "The title of the first movie:<br />";
echo $movies[0]["title"] . "<br /><br />";
echo "The director of the third movie:<br />";
echo $movies[2]["director"] . "<br /><br />";
echo "The nested array contained in the first element:<br />";
print_r( $movies[0] );
echo "<br /><br />";
?>