Multidimensional Array in PHP

Introduction

This article is about multidimensional arrays. An array provides the name of a declaring collection of data items that are of the same type using a single variable.  Each item within the array is uniquely distinguished using an index. If we ask what a PHP multi-dimension array is then the answer is "An array can also contain another array as a value, which in turn can hold another array. In such a way we can create two or three dimension arrays". The difference between one-dimensional and multidimensional arrays is that, a multidimensional array is a simple array that has simple arrays as elements, rather than strings or scalar variables. To use simple words, a multidimensional array is an array that contains one or more arrays.

Example of Multidimensional array in PHP

<?
php
$BooksofCsharpcorner =
array(

'String-C-Sharp'
=> array(
'Title'=>'String In C-Sharp',
'Author'=>'Mahesh Chand',
'Publication Date' => '2003'),

'SqlServer BOOK' => array(
'Title'=>'SQLServer 4.0',
'Author'=>'Mahesh Chand',
'Publication Date'=>'2011'),
 
'SharePoinr' => array(
'Title'=>'SharePoint 2010',
'Author'=>'Jean Paul',
'Publication Date'=>'2012'),
'WindowPhone' => array(

'Title'=>'Window Phone 7',
'Author'=>'Lbrahim Ersoy',
'Publication Date'=>'2012')
);
?>

Extracting an element from a multidimensional array

To extract a single element from a multidimensional array, you must use the key of both of the inner and outer arrays. The following example shows how to do it.
 

<?

echo "Title Of Sql Server Book: ". "<b>".$BooksofCsharpcorner['SqlServer BOOK']['Title']."</b>";

echo "<br>";

echo "Author of window Phone Book: "."<b>".$BooksofCsharpcorner['WindowPhone']['Author']."</b>";

?>
 

Output

Extracting-Element-from-multidimensional-array.gif

Iterating through a multidimensional array with foreach

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 ($BooksofCsharpcorner as $obj_key =>$book)

{

echo "$obj_key <b>Book</b>:<br>";

foreach ($book as $key=>$value){

echo "$key: $value<br>";

}

echo "<br>";

}

?>

 

Output

access-value-of-multidimension-array-through-loop.gif

Iterating through a multidimensional array with for

<?php
$sitedate = array( array('C-sharpcorner', 'Mahesh Chand', '2000'),
                   array('DotNetHeaven','Mahesh Chand', '2002') ); 

// $num_specials is 2: the number of elements in the first dimension of $specials
for
($i = 0, $num_specials = count($sitedate); $i < $num_specials; $i++) {
    // $num_sub is 3: the number of elements in each
sub-array

    for ($m = 0, $num_sub = count($sitedate[$i]); $m < $num_sub; $m++) {

        print "Element [$i][$m] is " . $sitedate[$i][$m] . "</br>";

    }

}

?>
 

Output

access-value-of-multidimension-array-through-for-loop.gif

Up Next
    Ebook Download
    View all
    Learn
    View all