Thursday, December 17, 2009

9:05 AM
Array is an single variable that holds multiple values. In an array you can store the related values into a single variable. 


If you have a list of items like (banana,apple,orange), then storing the fruits in a single variable could look like this:

$fruit1="banana";
$fruit2="apple";
$fruit3="orange";

 If you want to store 3 values is no problem. When it goes upto 100 or 200 then there may be a problem raised. The best solution to avoid this is to use an Array.

In PHP, there are three kinds of arrays. They are
            Numeric arrays - An array with numeric index
            Associative array - An array where each ID key is associated with a value.
            Multi dimensional array - An array containing one or more arrays.

Numeric arrays:
In PHP, there are 2 ways to create a numeric array

1. Index are automatically assigned. Example

   $fruits=array("banana","apple","orange");

2. Assign the indexes manually. Example
   $fruit[0]="banana";
   $fruit[1]="apple";
   $fruit[2]="orange";

Associative array:
In an associative array each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assign values to them.

There are 2 ways to create an associative array
1.,
$age = array("Kennedy"=>32, "Lincoln"=>30, "Bush"=>34);

2.,ID keys are used in a script
$ages['Kennedy'] = "32";
$ages['Lincoln'] = "30";
$ages['Bush'] = "34";

echo "Kennedy is " . $ages['kennedy'] . " years old.";


OUTPUT:
Kennedy is 32 years old.




0 comments: