Arrays

Arrays are indexed from 0.

12
The Cottage

Not necessay to add indexes:

Banana
Array ( [0] => Apple [1] => Pear [2] => Banana [3] => Grapes )

Associate Arrays

Associate Arrays - using strings as indexes

bat

Arrays in php are all associate - if you remove one value the indexes are not recalculated. The last values added are the last in the array whatever there index value.

Remove values with 'unset'

Use 'unset' to remove single array values or the entire array.

Array ( [first] => dog [third] => bat [fourth] => wolf )

You can mix data types in an array.

Array ( [0] => cat [1] => 123 [2] => 1.243 [3] => 1 )

Two dimensional arrays

PHP allows the creation of two dimensional arrays

Array ( [0] => Array ( [0] => item1 [1] => 4 [2] => 4.99 ) [1] => Array ( [0] => item2 [1] => 14 [2] => 0.99 ) [2] => Array ( [0] => item3 [1] => 1 [2] => 0.49 ) )

Outputing Array Values with foreach()

To output array values loop through them with foreach()

Andy
Band member: Johnny
Band member: Stephen
Band member: Andy
Band member: Mike
On Guitar we have Johnny
On Vocals we have Stephen
On Bass we have Andy
On Drums we have Mike