# PHP Two Dimensional Indexed Array

## PHP Two Dimensional Arrays – Part 1

Foreword: In this part of the series, I talk about PHP 2D indexed array.

By: Chrysanthus Date Published: 26 Jan 2014

### Introduction

This is part 1 of my series, PHP Two Dimensional Arrays. In this part of the series, I talk about PHP 2D indexed array. This is a tutorial.

Note: the output of all the code samples in this series are sent to the browser.

Note: in this article, if you cannot see any text or piece of code or if you think something (e.g. an image) is missing or link does not operate, or just want to comment, contact me at forchatrans@yahoo.com .

Pre-Knowledge
There are certain topics you need to have covered before you can understand this series. Click the link titled, “PHP Course” below to know what you should have studied before reaching here. This series is part of my volume, PHP Course.

Need for a Two-Dimensional Array
You need a two dimensional array when you want to keep information of tabular (table) nature. The following table has a list of products of a supermarket; the column headings are, ProductID Product Name, Category, Number, Cost Price and Selling Price

ProductID, ProductName, Category, Number, CostPrice, SellingPrice
1, TV Set, Entertainment, 50, 25, 30
2, DVD, Entertainment, 50, 20, 25
3, Clothe Box, Household, 45, 16, 21
4, Perfume, Beauty, 100, 2, 3
5, Banana, Fruit, 125, 5, 7
6, Pear, Fruit, 135, 3, 4

I will explain how to create a two-dimensional array that will hold all these data.

Indexing in a PHP Two-Dimensional Array
A two-dimensional array has rows and columns. Counting of rows begins from zero. Counting of columns also begins from zero. For the PHP Two-Dimensional Array, you type the row position (index) first before you type the column position, in order to identify a cell. So the value of [0][0] in the above table is, ProductID. This means the row concerned is row zero and the column concerned is column zero. The value of [1][0] in the table is, 1. The value of [0][1] is, ProductName. The value of [2][1] is, DVD; and so on.
Creating a Two-Dimensional Array
You create an empty two-dimensional array in the same way that you create an empty one-dimensional array. Let the name of our two dimensional array be arr. So you would create the empty two-dimensional array as follows:

\$arr;

At the moment it is just a variable.

Accessing an Array Element
You access an element in a two dimensional array with the following syntax:

\$arrayName[rowIndex][columnIndex]

Examples of its use are shown below.

Placing Elements into a 2D Array One-by-One
You can place elements into a two-dimensional array one-by-one. You do this using the square brackets and the corresponding indices (row and column numbers). The following code places the first and second rows of the above table into the array, one element at a time:

<?php

\$arr;

\$arr[0][0] = "ProductID";
\$arr[0][1] = "ProductName";
\$arr[0][2] = "Category";
\$arr[0][3] = "Number";
\$arr[0][4] = "CostPrice";
\$arr[0][5] = "SellingPrice";
\$arr[1][0] = 1;
\$arr[1][1] = "TV Set";
\$arr[1][2] = "Entertainment";
\$arr[1][3] = 50;
\$arr[1][4] = 25;
\$arr[1][5] = 30;

?>

Note: You can replace a value in a cell in the same way that you assign value for the first time. Also note the quotes used with strings above.
Reading Values from a 2D Array One-by-One
The syntax to read a value from a two-dimensional array into a variable is:

\$var = \$arrayName[rowIndex][columnIndex];

So you read a value in a similar way that you place a value. The following code would place the first two rows of the above table into an array and then print them out from the array.

<?php

\$arr;

\$arr[0][0] = "ProductID";
\$arr[0][1] = "ProductName";
\$arr[0][2] = "Category";
\$arr[0][3] = "Number";
\$arr[0][4] = "CostPrice";
\$arr[0][5] = "SellingPrice";
\$arr[1][0] = 1;
\$arr[1][1] = "TV Set";
\$arr[1][2] = "Entertainment";
\$arr[1][3] = 50;
\$arr[1][4] = 25;
\$arr[1][5] = 30;

for (\$i=0; \$i<2; ++\$i)
{
for (\$j=0; \$j<6; ++\$j)
{
echo \$arr[\$i][\$j] . ", ";
}
echo "<br>";
}

?>

Read and try the code. In a commercial program you would have to modify the code to remove the last comma displayed at the end of each row.

Placing Elements into a 2D Array Row-by-Row
You can place elements into a 2D array, one row at a time. The syntax to place a row into a 2D array is:

\$arrayName[rowIndex] = array(0=>value0, 1=>value1, 2=>value2, …);

The following program places all the rows of the above table into a 2D array and then displays the array and its content.

<?php

\$arr;

\$arr[0] = array(0=>"ProductID", 1=>"ProductName", 2=>"Category", 3=>"Number", 4=>"CostPrice", 5=>"SellingPrice");
\$arr[1] = array(0=>1, 1=>"TV Set", 2=>"Entertainment", 3=>50, 4=>25, 5=>30);
\$arr[2] = array(0=>2, 1=>"DVD", 2=>"Entertainment", 3=>50, 4=>20, 5=>25);
\$arr[3] = array(0=>3, 1=>"Clothe Box", 2=>"Household", 3=>45, 4=>16, 5=>21);
\$arr[4] = array(0=>4, 1=>"Perfume", 2=>"Beauty", 3=>100, 4=>2, 5=>3);
\$arr[5] = array(0=>5, 1=>"Banana", 2=>"Fruit", 3=>125, 4=>5, 5=>7);
\$arr[6] = array(0=>6, 1=>"Pear", 2=>"Fruit", 3=>135, 4=>3, 5=>4);

print_r(\$arr);

?>

Note that the string values are in quotes. Read and try the code, if you have not already done so.
Irregular Nature of a 2D Array
The above 2D array is regular in the sense that the edges of the array are straight. This means that you have equal number of columns (cells) for each row and equal number of rows (cells) for each column. In other words all the rows can have elements for each cell and all the columns can have elements for each cell. For the above array in the code, there are 6 cells per row and 7 cells per column; no cell is undefined or empty.

It is possible to have a 2D array in PHP that is irregular. For a 2D PHP array, you do not have to place a new element (value) in the next “available” cell. The following code illustrates this:

\$arr;

\$arr[0][0] = "00";
\$arr[0][2] = "02";
\$arr[1][3] = "13";
\$arr[2][1] = "21";

This code would be executed (would work). The array is irregular. This array would have been a 3-row-by-4-column array, judging from the maximum row and maximum column numbers. There are many cells, which are undefined for this array, e.g. cell [0][1].

2D Array by Initialization
Consider the following statement:

\$myArr = array(0=>"AAA", 1=>"bbb", 2=>"coconut");

The right operand here, can be considered as an array literal. The statement is an initialization statement. The literal was used in a code above to place elements into an array, row-by-row. “Array by initialization” is a phrase I have borrowed from the computer language, C++.

To create a two-dimensional array by initialization just place the array literals where the values should be in the one-dimensional array. Read and try the following code that illustrates this:

<?php

\$TDArrI = array(
0=>array(0=>"ProductID", 1=>"ProductName", 2=>"Category", 3=>"Number", 4=>"CostPrice", 5=>"SellingPrice"),
1=>array(0=>1, 1=>"TV Set", 2=>"Entertainment", 3=>50, 4=>25, 5=>30),
2=>array(0=>2, 1=>"DVD", 2=>"Entertainment", 3=>50, 4=>20, 5=>25),
3=>array(0=>3, 1=>"Clothe Box", 2=>"Household", 3=>45, 4=>16, 5=>21),
4=>array(0=>4, 1=>"Perfume", 2=>"Beauty", 3=>100, 4=>2, 5=>3),
5=>array(0=>5, 1=>"Banana", 2=>"Fruit", 3=>125, 4=>5, 5=>7),
6=>array(0=>6, 1=>"Pear", 2=>"Fruit", 3=>135, 4=>3, 5=>4)
);

echo \$TDArrI[2][1];

?>

You access an element here in the same way, as you do above. Note the semicolon at the end of the initialization construct. The rows are separated by commas.
Changing a Row
You can change a whole row. The syntax is:

\$arrayName[rowIndex] = array(New Elements);

In the following code the third row is changed:

<?php

\$TDArrI = array(
0=>array(0=>"ProductID", 1=>"ProductName", 2=>"Category", 3=>"Number", 4=>"CostPrice", 5=>"SellingPrice"),
1=>array(0=>1, 1=>"TV Set", 2=>"Entertainment", 3=>50, 4=>25, 5=>30),
2=>array(0=>2, 1=>"DVD", 2=>"Entertainment", 3=>50, 4=>20, 5=>25),
3=>array(0=>3, 1=>"Clothe Box", 2=>"Household", 3=>45, 4=>16, 5=>21),
4=>array(0=>4, 1=>"Perfume", 2=>"Beauty", 3=>100, 4=>2, 5=>3),
5=>array(0=>5, 1=>"Banana", 2=>"Fruit", 3=>125, 4=>5, 5=>7),
6=>array(0=>6, 1=>"Pear", 2=>"Fruit", 3=>135, 4=>3, 5=>4)
);

\$TDArrI[2] = array(0=>2, 1=>"3DG", 2=>"Amusement", 3=>10, 4=>10, 5=>10);

print_r(\$TDArrI);

?>

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Basics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
PHP Strings
Date and Time in PHP
PHP String Functions for Website Design
PHP Variable Scope
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Some features of PHP Entities
PHP Namespace
PHP Web Application