PHP Arrays: Array Functions and Multidimensional Arrays

January 26, 2012 | Miscellaneous, Web Development


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:

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

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

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

‘Horror’ => array(
‘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:

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’);

// $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.


Author Information
Utsav Singh Rathour
Utsav Singh Rathour is a blogger and developer for over 5 years and believes in building applications that not only tackles the problem but gives users a better interface. He is founder of Code Pixelz Media and blogs mainly at this place. Follow him at @rathour

Leave a Reply

WP Engine 15% off with NEWYEARNEWHOSTING15

  • Like what you see?

    We put a lot of effort to bring you best content. Please donate to help us survive.

  • Like us on Facebook

  • Tags