Tag Archives: data types

PHP Arrays

Arrays are sets of data; can be defined in a PHP Script. Arrays can contain other arrays inside them without any restriction, hence building multidimensional arrays. Arrays can be referred to as tables or hashes.

You can create Arrays in two different ways. The first involves using the function array. The second involves using square brackets.

First is The Array Function Method

In the array function method you create a array in the scheme of:

$foo = bar()

For example, to set the array up to make the keys sequential numbers (Example: “0, 1, 2, 3”), use:

$foobar = array($foo, $bar);

This would produce the array as follows:

$foobar[0] = $foo
$foobar[1] = $bar

It is also possible to define the key value:

$foobar = array(‘foo’ => $foo, ‘bar’ => $bar);

This would set the array as follows:

$foobar[‘foo’] = $foo
$foobar[‘bar’] = $bar

And,

Second is The Square Brackets Method

The square brackets method allows you to set up by directly setting the values.

For example:

To make $foobar[1] = $foo, what you should do is:

$foobar[1] = $foo;

The same applies for setting up the key value:

$foobar[‘foo’] = $foo;

Other Array Lessons: