php.w3clubs.com

PHP tutorials and articles

 

Arrays - part 1

In addition to the strings, numbers and booleans, there is another data type - the array. Arrays are really useful and powerful so it's a good idea to learn them well.

Enumerated lists

An array is a list of values. The values in the list can be of any type, including other arrays. Here's one array - it's a list of odd numbers.

$myarray = array(1, 3, 5, 7, 9);

As you can see, in order to define an array, you use the language construct array(), which looks just like a normal function. Elements inside an array are separated by commas.

In order to access an individual element of the array, you use square brackets and in the brackets you specify the position of the element you want, starting from zero.

echo $myarray[0]; // prints 1, the first element
echo $myarray[1]; // prints 3, the second element
echo $myarray[4]; // prints 9, the fifth (4 + 1) element

The elements of the list are identifiable by their key, this is the number you put in square brackets. The keys are automatically created starting from zero and incrementing. The first element has key 0, the second has key 1 and so on. Let's see a little table that illustrates the array we used above.

Key Value Access using
0 1 $myarray[0]
1 3 $myarray[1]
2 5 $myarray[2]
3 7 $myarray[3]
4 9 $myarray[4]

Adding an element to an array

To add a new element at the end of the array, just use square brackets without a value, like so:

// array with three elements
$a = array(1, 2, 3);
// adding a fourth element
$a[] = 4;
// adding one more element
$a[] = 5;
echo $a[4]; // prints the fifth element, value 5

Instead of using empty brackets, you can also specify an exact index when you add to an array, or modify an element.

// array with three elements
$a = array(1, 2, 3);
// adding a fourth element
$a[3] = 4;
// adding the hundredth element
$a[99] = 100;

It's not an error to skip indexes, but maybe not very practical most of the times.

If you attempt to use an element with a non-existing key, you'll get a PHP notice that this key doesn't exist.

// array with three elements
$a = array(1, 2, 3);
echo $a[88]; // throws a notice

Removing elements

You can remove elements from an array, using the unset() function.

// array with three elements
$a = array(1, 2, 3);
unset($a[1]); // remove the second element
echo $a[0]; // prints 1
echo $a[1]; // Notice: undefined offset,
            // meaning that this element doesn't exist
echo $a[2]; // prints 3

BTW, the unset() function can also be used to unset variables, as if they were never defined.

$a = 1;
echo $a; // prints 1
unset($a);
echo $a; // Notice: undefined variable $a

Mixed values

In the examples above we only stored integers in the arrays, but you can store any type of data.

$a = 1;
$b = "b";
$myarray = array(
  $a,
  $b,
  99.99,
  "Ninja!",
  array(11, 22, array(9, 8, 7));
);
// let's print!
echo $myarray[0]; // prints 1
echo $myarray[1]; // prints b
echo $myarray[2]; // prints 99.99
echo $myarray[3]; // prints Ninja!
echo $myarray[4][0]; // prints 11
echo $myarray[4][1]; // prints 22
echo $myarray[4][2][0]; // prints 9
echo $myarray[4][2][1]; // prints 8
echo $myarray[4][2][2]; // prints 7

Using two pairs of square brackets [][] you can access arrays contained within arrays. This way you can nest arrays as deep as you need.

Here's for example an array structure that contains a 3 by 3 grid of a tic-tac-toe game;

$ttt = array(
  array('x', 'x', 'o'),
  array('o', 'x', 'o'),
  array('x', 'o', 'x'),
);
// getting the cell in the middle
echo  $ttt[1][1]; // prints x

Note on syntax - it's OK to have a trailing comma after the last element, it's not an error (as long as it's only one comma).

$a = array(1, 2, 3); // this is OK
$a = array(1, 2, 3,); // this is OK
$a = array(1, 2, 3,,); // parse error

Summary

  1. Arrays are important, you'll see much more examples of their use in the following articles.
  2. An array is a list of values, values are indexed by keys
  3. You can store anything in an array, including other arrays

« to the PHP tutorial table of contents

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress, theme by ♣w3clubs.com, based on Kubrik and Sancta Simplicitas