PHP Arrays

                 An array is a data structure that stores one or more values in a single value. Despite of other normal variables an array can store more than one value.

Three types of array are used.

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

Numeric array :

                 Let's suppose you want to store basic days in your PHP script. You can construct a small list from them like this

	$a ='sun';
	$b ='mon';
	$c ='tue';
	$d ='wed';
	$e ='thu';

                 It is quite hard, boring, and bad idea to store each color in a separate variable. It would be very nice to have the above representation almost as it is in PHP. And here comes array into play.

                 The array type exists exactly for such purposes. So let's see how to create an array to store our color list.

                 There are several ways to create an array. Some ways are..

	1)$days = array("sun","mon","tue","wed","thu");

It is also declared as

	2)$days[0] = "sun";
	  $days[1] = "mon";
	  $days[2] = "tue";
	  $days[3] = "wed";
	  $days[4] = "thu";

Display Elements:

                 If you want only display one element of the array then you can just write the following code.

echo $days[0]

It will shows sun as output.

                 If you want only display All element of the array then you can just write the following code..

<?php 
	$days = array("sun","mon","tue","wed","thu");
	foreach ($days as $value) {
	echo '<br/>'.$value;
	}
?>
Output : 
sun
mon
tue
wed
thu
Note :For each is a special type of conditional statement. It is used especially in arrays.

Associative Array :

                 In general PHP arrays are maps, which means that it is a type that maps values to keys. In above example 0 represents 'sun' and 1 represents ‘mon’ and so on..

                 Associative array means that you can assign an arbitray key to every value. Associative arrays are sometimes referred to as dictionaries.

                 For example Storing the salaries of different employees by using array is shown in below..

Ex:
$salary = array('sam'=>1000, 'ram'=>2000, 'john'=>5000, 'don'=>4000);

It is also write as :

$salary['sam'] = "1000";
$salary['ram'] = "2000";
$salary['john'] = "5000";
$salary['don'] = "4000";

                 If we want to display the salary of john the write the following code.

    Echo $salary['john'];
functions.php
<?php 
$salary = array('sam'=>1000, 'ram'=>2000, 'john'=>5000, 'don'=>4000);	
echo "The salary of ram is ".$salary["ram"];
?>

Output : The salary of ram is 2000

Some useful functions of array:

Sizeof():

                 Get the length of the array, or with other words how many elements are in the array. To get this information you can use the sizeof function. It tells you how big is the actual data. You can use it like this

    echo sizeof($salary);

Unset():

                 Sometimes you want to remove an element from the array. In this case you can use the unset function like this

    unset($salary ["ram"]);

Isset():

                 To check whether an array has a requested element you can use the isset function as follows

    if (isset($salary["don"])) echo "existed";

Sorting an array:

                 And last sometimes you want to sort your array for eaxmple to display its content in alphabetical order. To do this you have more possibilities. There are more built in array sorting functions in PHP. The most known are sort and asort. The difference between them is that sort renumbers the keys so you will lost the key values. So if you need the key names (associative arrays) use the asort function.

You can use them as follows:

1.	asort($salary);
2.	sort($salary);

                 Both of the functions sorts values in ascending order. If you want to sort them descending use the corresponding rsort and arsort functions.