An array is a special kind of variable that contains multiple values.The simplest way to create an array in PHP is to use the array command:
$myArray = array('one', 2, '3');
This code creates an array called $myArray that contains three values: ‘one’, 2, and ‘3’. Just like an ordinary variable, each space in an array can contain any type of value. In this case, the first and third spaces contain strings, while the second contains a number. To access a value stored in an array, you need to know its index. Typically, arrays use numbers as indices to point to the values they contain, starting with zero. That
is, the first value (or element) of an array has index 0, the second has index 1, the third has index 2, and so on. Therefore, the index of the nth element of an array is n–1. Once you know the index of the value you’re interested in, you can retrieve that value by placing that index in square brackets after the array variable name:
echo $myArray; // outputs 'one' echo $myArray; // outputs '2' echo $myArray; // outputs '3'
Each value stored in an array is called an element of that array. You can use an index in square brackets to add new elements, or assign new values to existing array elements:
$myArray = 'the fifth element'; echo $myArray; // outputs 'the fifth element'
well these type of arrays are called numeric arrays, as its indexes are all numeric.There is yet another type of array called associative arrays which uses characters or strings as index infact this type of arrays are more meaningful for example look at the code below :
$birthdays = array('Kevin' => '1978-04-12', 'Stephanie' => '1980-05-16', 'David' => '1983-09-09');
here we created an array $birthdays with three indexes ‘kevin’,’stephanie’ & ‘David’ and we use ‘=>’ to store the corresponding birthdays.if we want to retrieve kevins’s birthday we simply have use its corresponding key or index like this :
echo 'Kevin birthday is: ' . $birthdays['Kevin'];
now just change the key to get the corresponding birthday and you will get the corresponding birthdays simple.
if you want to know more about php array head to php.net its the official site of php.
See you next time with more helpful tutorials on php and web if you have any questions post a comment…