Arrays will probably be one of the data structures you will use most in PHP. It is useful for a variety of different purposes. An array is declared like so..
$names = array();
You can pre-populate an array with the following syntax.
$names = array('tom', 'john', 'dominic');
You can read an array element by extracting the value based on its index. So...
$names = array('tom', 'john', 'dominic');
echo $names[0];
//will echo tom as its the first element in the array.
for ($i=0;$i < count($names);$i++)
 echo $names[$i];
//The for loop will echo:
Notice above that we use the count function this gives us the number of elements in a array. Or you can add values at run time in the following fashion.
$names[] = 'robert';
You also don't have to only add strings to arrays, you can add objects, integers, other arrays in fact almost any other value. The [] indicates an empty index for this array, which is therefore defaulted to a integer which will start a zero and be incremented for each element added. PHP allows you to also specify your own index be it a integer or a string.

Associative arrays

You can create a associative by using a arbitary value as the arrays key.
$ages['robert'] = 28;
Furthermore you can also pre-populate an associative array in the following fashion.
$ages = array('tom'=>23, 'john'=>30, 'dominic'=>22);
The array keys are case sensitive so "Tom" is different to "tom" but like a lot of PHP functionality it is type insensitive so "5" is equal to 5! You can also mix different types so numbers and strings for instance but you need to be careful when extracting the values.
$ages = array('tom'=>23, 'john'=>30, 'dominic'=>22);
echo $ages['john'];
//will echo 30 as its the first element with the array key john
One of the easiest ways to read an array is with a foreach loop below both the keys and values of the loop are read and outputted.
$ages = array('tom'=>23, 'john'=>30, 'dominic'=>22);
foreach ($ages as $key => $value)
 echo $key . ' - ' . $value;

Multidimensional arrays

You can have arrays within arrays which will create a nested structure. Below is an example of a multidimensional array
$people = array('robert'=>array('male', 23), 'julie'=>array('female', 26));
You can access a multidimensional array by supplying the key for each dimension, as shown...
$people = array('robert'=>array('male', 23), 'julie'=>array('female', 26));
echo $people['julie'][0];
//echo's female;
A useful function to see what is present in arrays or indeed objects is the print_r function, so for the
array it will show the following...
Array ( [robert] => Array ( [0] => male [1] => 23 ) [julie] => Array ( [0] => female [1] => 26 ) )

Useful array functions


This will remove a element from an array like so...
$ages = array('tom'=>23, 'john'=>30, 'dominic'=>22);


This function checks if a array has a requested array, this is useful to ensure that you don't try to do something with a value that doesn't exist. For instance...
$ages = array('tom'=>23, 'john'=>30, 'dominic'=>22);
if (isset($ages['tom']))
 //do the following.

No comments:

Post a Comment