Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Two-dimensional arrays in PHP

Wednesday, August 6th, 2014 by Servage



There’s a lot more you can do with arrays. For example, instead of being single-dimensional lines of matchboxes, they can be two-dimensional matrixes or can even have three or more dimensions.

As an example of a two-dimensional array, let’s say we want to keep track of a game of tic-tac-toe, which requires a data structure of nine cells arranged in a 3×3 square. To represent this with matchboxes, imagine nine of them glued to each other in a matrix of three rows by three columns.

You can now place a piece of paper with either an “x” or an “o” in the correct matchbox for each move played. To do this in PHP code, you have to set up an array containing three more arrays, as in Example 3-5, in which the array is set up with a game already in progress.

Defining a two-dimensional array

$oxo = array(array('x', '', 'o'),
array('o', 'o', 'x'),
array('x', 'o', '' ));

Once again, we’ve moved up a step in complexity, but it’s easy to understand if you grasp the basic array syntax. There are three array() constructs nested inside the outer array() construct.

To then return the third element in the second row of this array, you would use the following PHP command, which will display an “x”:

echo $oxo[1][2];

Remember that array indexes (pointers at elements within an array) start from zero, not one, so the [1] in the previous command refers to the second of the three arrays, and the [2] references the third position within that array. It will return the contents of the matchbox three along and two down from the top left.

Variable naming rules

When creating PHP variables, you must follow these four rules:

• Variable names must start with a letter of the alphabet or the _ (underscore) character.

• Variable names can contain only the characters a-z, A-Z, 0-9, and _ (underscore).

• Variable names may not contain spaces. If a variable must comprise more than one word, the words should be separated with the _ (underscore) character (e.g.,$user_name).

• Variable names are case-sensitive. The variable $High_Score is not the same as the variable $high_score.


Operators are the mathematical, string, comparison, and logical commands such as plus, minus, times, and divide, which in PHP looks a lot like plain arithmetic; for instance, the following statement outputs 8:

echo 6 + 2;

Before moving on to learn what PHP can do for you, take a moment to learn about the various operators it provides.

Arithmetic operators

Arithmetic operators do what you would expect. They are used to perform mathematics. You can use them for the main four operations (plus, minus, times, and divide), as well as to find a modulus (the remainder after a division) and to increment or decrement a value.

Operator Description Example

+ Addition $j + 1

– Subtraction $j – 6

* Multiplication $j * 11

/ Division $j / 4

% Modulus (division remainder) $j % 9

++ Increment ++$j

−− Decrement −−$j

Sources for further reading

Two-dimensional arrays in PHP, 3.0 out of 5 based on 2 ratings
Categories: Guides & Tutorials, Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.