In this PHP tutorial I described variables as rock sacks but actually a variable can be described as a digital version of a locker that you can store almost anything in it, the problem is that it can only hold ONE thing and any given time. So what if you needed to hold multiple values? well that’s why we have Arrays. If a variable is a locker then an Array can be described as a locker room, An array holds multiple Items at a same time and all you need to access them is to know where they are or in another word what they are called.

Setting up an array couldn’t be any easier here is the code:

$array_name=array();// creates an empty array (defines an array)

$array_name[]=new_array_value;//it adds one item to the array so if :

$a=new Array();//$a is an empty array

$a[]=12; //$a has one value stored in it now, which is 12 and this value would be in position 0 because there were nothing before this and numbers in array ALWAYS start from 0.

$b[“a key here”]=”some text”;// this kind of array is known as the associative array because you associate a value to a key

Here is another way :

$array_name=new Array(

“number 1″=>12,

“some text”=>”this one is a text”,

“some Boolean“=>false

);// you can define it and give values in one place this way.


$array_name can be any name as long as it follows the rules about variable names and also is not one of the reserved names you will find a reserved keywords and names here

Array_key is a key that you will use to identify the specific item inside the array it can be a number(the default and it starts from 0) or it can be a string. Look at the examples

array_value is a value that you put in that specific item

OK we set them up now how we access them and change them well pretty much the same way:




So you can have :

$b=array(3,4,7,12,78,99);// this is another simple way to create the array

$a=$b[2];//$a = 7 because the numbers starts from 0 so $b[0]=3 , $b[1]=4 , $b[2] = 7 and so on…


To empty an array you can just set it to nothing like this:

$a=array(1,4,7,23,12,11);// just set it up

$a=new Array(); //just emptied it



unset($a[2]);// just deleted the $a[2] item.

$a[1]=null; //just deleted item $a[1].

Now one of the coolest things about an Array is that you can store other arrays inside them that will make them multi dimensional arrays. remember the locker room example well now imagine that the lockers are in rows(and yes they usually are but just bear with me here) now to identify a locker you would say room 5, 2nd row, 5th locker on the right now in PHP it would be something like:

room_5[2nd row,5th locker,on right]=my stuff;

in the above example room_5 is the name of the locker room or array_name, 2nd row is another array that has lockers in right and left, on right is another array of lockers that are in the right side.

Here are some examples:

$a=array(1,2,3);//one dimensional array(normal array)

$b=array(4,5,6);//one dimensional again

$c=array($a,$b);// two dimensional as it holds two arrays so if i would show you $a here is how it would look like:

key 0 1 2
value 1 2 3

and $b would be:

key 0 1 2
value 4 5 6

and $c would be:

key value key value key value
00 1 01 2 02 3 $c[0] array is the $a array
10 4 11 5 12 6 $c[1] array is the $b array

An you can have as many dimensions as you like added to this for example in the above example $a itself could hold another arrays and they could have array in them and so on…

Arrays are a very useful feature and available in most programming languages(including PHP) but when it comes to web development you can do very cool stuff with them like; if you have a form that users can enter what they need a quote from you for and you them have to respond to them in normal HTML there is nothing that would allow them to add more items and fields to the form, and here comes the array! all you have to do is set a counter and the top and every value in the fields to an array add a bit of creative programming and you have an add-able form using PHP.

In-fact arrays are so good that PHP uses it itself, remember variables well one of the types of the values that you can store in a variable is string and string in fact is array of characters and you can even access it the same way.

%d bloggers like this: