# Array formation in PHP

We have talked about array in one of our previous articles. But we gave very little information about array in this programming language. In this article we will discuss the array in detail.

Array is one of the most important kinds of variables. Why so! Because using an array system we can put two or more information into a single array variable. We can call an as a container of variables. Now we can also use a single variable from an array. Let’s begin with how an array looks!

$array = array(“ab”, “bc”. “cd”);

A simple array usually looks like this. We can see that this array have three components in it. These are ab, bc and cd. Now we can show these arrays using echo or print function.

Another kind of array is multidimensional array. An multidimensional array means two or more array within an array.

$mularray =array (

array (“a”,”b”, ”c”),

array (”ab”,”bc”,”cd”)

);

Also we can create array using several variables. Suppose we have 5 variables we can put them into one single array. This is also another way to create an array.

$a = 1;

$b = 2;

$c = 4;

$d = 6;

$e = 9;

Now, if we want to create an array using these variables then we can do it as shown below.

$ab = array ($a, $b, $c, $d, $e);

If we print the last variable then we will see it is an array.

Now let’s see how we can retrieve the values from an array in PHP programming.

to understand the retrieve process we need to understand the key value formation of an array used in PHP. Let’s take $ab variable to understand the basic key value relation. Usually the key starts from zero (0) by default. But we also can choice the value of the key if we want.

The $ab variable will be like

Array (

[0] => 1

[1] => 2

[2] =>4

[3]=>6

[4]=>9

)

Here the numbers in third parenthesis is the key and the right side value is the variable value. Now if we need the third value of the variable then we can simply call it by $ab[2]. To print or echo this variable we need to run the below code.

echo $ab[2];

the result will be 4.

Now let’s think about the multidimensional array. There we can see there are two arrays into one array. Suppose we want to see the value “bc”, then we need to do a little calculation to find this. The first array will have the key “0” and the second array will have primary key “1”.

Again the value “bc” is in the second array and the “bc” value contain the second place in that array. So the final array will be

$mularray[1][1];

Just run the below command.

echo $mularray[1][1];

now we will get the result “bc”.

This way we can calculate array. On our next post we will discuss about how we can retrieve array values separately.