Arrays

Array is set of variables which members essentially does not have to be of the same type. It's a collection or list of variables, put together. They have values that associate to keys. Keys may only be either integer or string, while values can be anything including arrays. Arrays can be simple(numeric and associative) e and multidimensional. The syntax for creating array is:

<?php
$ar=array([key=>]value[,key=>value,]...);
?>

Ex.1 Creating array
<?php
$ar=array(1,2,3,4,5,6,7,8,9);
//will create the array:
//and print_r($ar) will give
?>

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)

The above code is same as:
<?php
$ar=array(0=>1,2,3,4,5,6,7,8,9); //you can set whether the first key will be 0,1 or some text
//php automatically assign subsequent key values
?>

More complex array:
<?php
$ar=array('bar'=>'baz','narr'=>array('bar','baz'=>array('bar'=>'baz')));
//which prints
?>

Array
(
    [bar] => baz
    [narr] => Array
        (
            [0] => bar
            [baz] => Array
                (
                    [bar] => baz
                )

        )

)

Sometimes you want to add element but do not know whats the current key value. To add automatically an element to the array with key +1 use this code:
<?php
//the array is
$ar=array(1,2,3,4,5,6,7,8,9);
//adding new element
$ar[]=10; //$ar[9]=10;
?>
Or if would it be the first key added then the key will have value of 0.

To delete element or whole array use unset()

<?php
//the array is
$ar=array(1,2,3,4,5,6,7,8,9,10);
unset($ar[5]); //removes $ar[5] element with value 6
unset($ar); //deletes whole array
?>

To get the number of items in an array use the function count().
To traverse the arrays you can use while, for, foreach (if you don't know the array length), array_map.

<?php
//the array is
$ar=array(1,2,3,4,5,6,7,8,9,10);
foreach($ar as $key=>$value) //will display 1122334455667788991010
{
echo $ar[$key];
echo $value;
}

//example with array_map
function funk($v)
{
echo $v;
echo $v;
$v=20;
return $v;
}
$ar=array_map("funk",$ar); //will display 1122334455667788991010
// array $ar is changed:
//$ar[0]=$ar[1]=....$ar[9]=20;
?>

array_map() returns an array with function func applied to all the elements of the supplied array.