PHP Arrays: Array Functions and Multidimensional Arrays

In Part I, we looked at simple arrays, as well as how to loop through and sort array elements.  In this article, we will look at other array functions as well as multidimensional arrays.

The difference between one-dimensional and multidimensional arrays is a simple one: a multidimensional array is a simple array that has simple arrays as elements, rather than strings or scalar variables.

Building a Multidimensional Array

Here is how our $arrBooks example from last week’s article can be expanded into a multidimensional array:

<?php
$arrBooks = array(
‘Comic’ => array(
‘Title’=>‘Superman’,
‘Author’=>’Jerry Siegel and Joe Shuster’,
‘Publication Date’ => ‘1938’),

‘Science Fiction’ => array(
‘Title’=>‘Dune’,
‘Author’=>’Frank Herbert’,
‘Publication Date’=>’1965’),

‘Fantasy’ => array(
‘Title’=>‘The Hobbit’,
‘Author’=>’J.R.R. Tolkien’,
‘Publication Date’=>’1937’),   

‘Horror’ => array(
‘Title’=>‘Carrie’,
‘Author’=>’Stephen King’,
‘Publication Date’=>’1974’)
);
?>

Extracting Elements from a Multidimensional Array

To extract a single element from the multidimensional array, you must refer to the keys in both the outer and inner arrays. For instance, the PHP code below:

<?
echo $arrBooks[‘Science Fiction][‘Title’];
echo "<br>";
echo $arrBooks[‘Horror’][‘Author’];
?>

would display:

Dune
Stephen King

Looping Through a Multidimensional Array

The easiest way to loop through a multidimensional array is to nest two foreach loops; the outer loop goes through each outer array element, and the inner loop goes through each inner array element within the selected outer element.

<?
foreach ($arrBooks as $obj_key =>$book)
{
echo "$obj_key Book:<br>";
foreach ($book as $key=>$value){
echo "$key: $value<br>";
}
echo "<br>";
}
?>

The display will look like this:

Comic Book:
Title: Superman
Author: Jerry Siegel and Joe Shuster
Publication Date: 1938

Science Fiction Book:
Title: Dune
Author: Frank Herbert
Publication Date: 1965

Fantasy Book:
Title: The Hobbit
Author: J.R.R. Tolkien
Publication Date: 1937

Horror Book:
Title: Carrie
Author: Stephen King
Publication Date: 1974

Array Functions

Arrays are one of the most useful variable types.  Along with its versatility, arrays also can use a variety of functions.  In the previous lesson, we used the is_array function to determine if a variable was an array and the sort function to sort the elements of an array.  Here are some more examples of array functions.

count($array): Counts the number of elements in an array.

<?
$numBooks = count($arrBooks);
echo "There are $numBooks books in the collection.<br>";
?>

There are 4 books in the collection.

extract($array): Converts associative array keys into string variables. The values of each key become the values of each variable.

<?
$arrBooks = array(    ‘Comic’ => ‘Superman’,
‘ScienceFiction’ => ‘Dune’,
‘Fantasy’ => ‘The Hobbit’,
‘Horror’ => ‘Carrie’);

extract($arrBooks);
// $arrBooks[‘Comic’] becomes $Comic
// $arrBooks[‘ScienceFiction’] becomes $ScienceFiction
// $arrBooks[‘Fantasy] becomes $Fantasy
// $arrBooks[‘Horror] becomes $Horror

echo "$Comic is a comic book.<br>";
echo "$Fantasy is a fantasy book.<br>";

?>

Superman is a comic book.
The Hobbit is a fantasy book.

extract($array, EXTR_PREFIX_ALL, ‘prefix’): Adds a prefix to the string variable to differentiate between arrays that have the same keys.

<?
$arrBooks = array(
‘Comic’ => ‘Superman’,
‘ScienceFiction’ => ‘Dune’,
‘Fantasy’ => ‘The Hobbit’,
‘Horror’ => ‘Carrie’);

extract($arrBooks, EXTR_PREFIX_ALL, "books");
// $arrBooks[‘Comic’] becomes $books_Comic
// $arrBooks[‘ScienceFiction’] becomes $books_ScienceFiction
// $arrBooks[‘Fantasy] becomes $books_Fantasy
// $arrBooks[‘Horror] becomes $books_Horror

echo "$books_Comic is a comic book.<br>";
echo "$books_Fantasy is a fantasy book.<br>";

$arrFilms = array(
‘Comic’ => ‘Superman Returns’,
‘ScienceFiction’ => ‘Terminator’,
‘Fantasy’ => ‘Dark Crystal’,
‘Horror’ => ‘Friday the 13th’);

extract($arrFilms, EXTR_PREFIX_ALL, "films");
// $arrFilms [‘Comic’] becomes $films_Comic
// $arrFilms [‘ScienceFiction’] becomes $films_ScienceFiction
// $arrFilms [‘Fantasy] becomes $films_Fantasy
// $arrFilms [‘Horror] becomes $films _Horror

echo "$films_Comic is a comic film.<br>";
echo "$films_Fantasy is a fantasy film.<br>";
?>

Superman is a comic book.
The Hobbit is a fantasy book.
Superman Returns is a comic film.
Dark Crystal is a fantasy film.

compact(var1, var2, var3): Converts a list of variables into an array.

<?
$Comic = ‘Batman’;
$ScienceFiction = ‘Dreaming Void’;
$Fantasy = ‘American Gods’;
$Horror = ‘Frankenstein’;

$arrBooks2 = compact (‘Comic’, ‘ScienceFiction’, ‘Fantasy’, ‘Horror’);

foreach ($arrBooks2 as $key => $value) {
print  "$value is an example of a $key book.<br>\n";
}
?>

Batman is an example of a Comic book.
Dreaming Void is an example of a ScienceFiction book.
American Gods is an example of a Fantasy book.
Frankenstein is an example of a Horror book.

In the next lesson, we will look at more array functions, including functions that “push”, “pop”, “shift”, “unshift” and “merge” arrays.

Gerald Hanks has been involved in web development applications since 1996. He has designed applications with JavaScript, ASP.NET and PHP, as well as building databases in MS SQL Server and MySQL. He lives in Houston, Texas. More articles by Gerald Hanks
Home CSS Deals DesignBombs HTML HTML5 JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress