Arrays

Posted by tutor | Posted in PHP Tutorial | Posted on 03-08-2010-05-2008

0

An array is a collection of similar type of values. It is a data structure which stores one or more similar type values as a single value. In PHP arrays are actually maps, ie each key is mapped to a value.

We are assuming that you have a basic knowledge of programming, so you must be knowing what are arrays.

If you are new to programming, then it may confuse you, as till now one variable can store only one value but this variable can store multiple values.

You may think an array as a container with many spaces where you can fill data according to your choice. Arrays group similar kind of data as it doesn’t make sense to store a set of like values in different variables.

For eg if you want to store your marks of different subject, if you have only 5 subjects then you can store them separately, but if you are having twenty or thirty subjects or even more then it will look very clumsy to use so much variables moreover it will be very tough to manage such number of variables.

You can define array using square brackets as:

$my_array[];

You can assign values to the array elements as:

$marks[0]=20;
$ marks [1]=10;

Araay can be understood using the key / value structure. Keys are the numbers specified in the array and the values are the marks. Each key of an array is associated with a value that we can manipulate and reference.

The general form to set the key of an array equal to a value is:

$array[key] = value;

In other programming language you can only provide integer as the key value for the array but in PHP you can also specify a string as the key. This later type is referred to as Associative array

PHP arrays are mostly used with loops, which we will be discussing soon.

In PHP you have the following types of arrays:

  • Numeric array – An array with a numeric index
  • Associative array – An array where each ID key is associated with a value
  • Multidimensional array – An array containing one or more arrays

Numeric Arrays

A numeric array is the most common type of array, which stores each array element with a numeric index. This kind of array is found in all programming languages.

There are two methods to create a numeric array.

  1. In the following example the index are automatically assigned (the index starts at 0)<$marks=array(20,10,30,40)
  2. In the following example we assign the index manually:$marks[0]=20;
    $ marks [1]=10;
    $ marks[2]=30;
    $ marks [3]=40;

Associative Arrays

In an associative array the key used is a string value. We have already seen how you can store the marks of the subjects you studied till now using an array. But there was a slight problem, you will not be able to distinguish which mark is for which subject unless you write it somewhere that which key value stands for which subject. This is extra information you will have to manage somewhere outside your program. Rather if there have been a way to store them together then it will lessen your burden.

Associative array is such a way. The key values of this array are strings.

$marks[“Physics”]=20;
$marks[“Chemistry”]=30;
$marks[“Biology”]=40;
$marks[“Maths”]=60;

Multidimensional Arrays

A multidimensional array is an array of arrays. In a multidimensional array, each element in the main array is also be an array. And each element in the sub-array can be an array, and so on.

$families = array (“Fruit”=>array (“Mango”,”Apple”,”Strawberry”),
“Animals”=>array (“Lion”,”Tiger”),
“Birds”=>array (“Eagle”,”Peacock”,”Penguin”)
);

The arrays and sub-arrays can be referred to as

$families['Fruit'][2] will refer to Strawberry.