Facebook Page Twitter Page LinkedIn Page
× PHP Q&A


A multi-dimensional array is an array that contains arrays as an element. For example - A 2-dimensional array is an array of arrays and 3-dimensional array is an array of arrays of arrays.

Create a Multi-dimensional Array

A multi-dimensional array can be created using array() keyword with each element created using array() keyword.

Syntax

//Creating a 2-dimensional array.
$MyArray = array(array(), array(), ...);  

//Creating an array containing three elements.
$MyArray = array(1, 2, 3); 

Access element of a Multi-dimensional Array

An element of an Array can be accessed with it's index number. Index number for an Array in PHP starts with 0.

PHP Arrays Indexing

Below mentioned example illustrates on indexing for an Array in PHP.

<?php
  //creating an array with 3 elements
  $MyArray = array('MON', 'TUE', 'WED');

  //Add 4th and 5th elements
  $MyArray[3] = 'THU';
  $MyArray[4] = 'FRI';
  
  echo "MyArray contains: ";
  for ($i = 0; $i < count($MyArray); $i++)
  {
    echo $MyArray[$i]." ";
  }
?>

The output of the above code will be:

MyArray contains: MON TUE WED THU FRI 

Change elements of an Array

Any element of an Array can be changed using its index number and assigning a new value.

<?php
  //creating an array with 5 elements
  $MyArray = array(10, 20, 30, 40, 50);

  //changing the value of element at index=1
  $MyArray[1] = 1000;
  
  echo "MyArray contains: ";
  for ($i = 0; $i < count($MyArray); $i++)
  {
    echo $MyArray[$i]." ";
  }
?>

The output of the above code will be:

MyArray contains: 10 1000 30 40 50

Loop over an Array

By using for loop, while loop or foreach loop, each elements of an array can be accessed.

For Loop over an Array

In the below example, for loop is used to access all elements of an array.

<?php
  //creating an array with 5 elements
  $MyArray = array(10, 20, 30, 40, 50);

  echo "MyArray contains: ";
  for ($i = 0; $i < count($MyArray); $i++)
  {
    echo $MyArray[$i]." ";
  }
?>

The output of the above code will be:

MyArray contains: 10 20 30 40 50

While Loop over an Array

In the below example, while loop is used to access all elements of an array.

<?php
  //creating an array with 5 elements
  $MyArray = array(10, 20, 30, 40, 50);

  $i = 0;
  echo "MyArray contains: ";
  while($i < count($MyArray))
  {
    echo $MyArray[$i]." ";
    $i++;
  }
?>

The output of the above code will be:

MyArray contains: 10 20 30 40 50

Foreach Loop over an Array

In the below example, foreach loop is used to access all elements of an array.

<?php
  //creating an array with 5 elements
  $MyArray = array(10, 20, 30, 40, 50);

  echo "MyArray contains: ";
  foreach ($MyArray as $i)
  {
    echo $i." ";
  }
?>

The output of the above code will be:

MyArray contains: 10 20 30 40 50

Multi-dimensional Array

Multi-dimensional array can be viewed as arrays of arrays. PHP allows you to define array of any dimension.

Syntax

int MyArray [n][m];    //2-dimension array.
int MyArray [p][q][r]; //3-dimension array.
int MyArray [2][3] = {10,20,30}; //2-dimensional array with size (2,3).

#include <iostream>
using namespace std;
 
int main (){
    int numbers[2][3] = {10, 20, 30, 40, 50, 60};
    for(int i = 0; i < 2; i++)
    {
      for(int j = 0; j < 3; j++)
      {
        cout<<numbers[i][j]<<" ";
      }
      cout<<"\n";
    }
    return 0;
}

The output of the above code will be:

10 20 30 
40 50 60