Array is an orderly arrangement of data. When multiple data are grouped together and assigned by a variable then that variable is known as an array. A simple example of an array is a text string. In a text string all characters are orderly arranged to form the string. So a text string is a character array. In PHP the language constructor array() is used to declare an array. Arguments of array() constructor are comma separated data contain in the array. Not all other languages but PHP allow mixed data (i.e. different types of data) in an array. Here are few examples of PHP array.
<?php
$myvariable=array(); //Declaration of an empty array.
$myvariable=array(4,5,9,2,5); //Declaration of an array containing integer data. Data are separated by comma.
$myvariable=array(4,5,'three',2,5); //Declaration of mixed variable array.
// Not all other languages but PHP allows different types of data in an array.
$myvariable=array('color'=>'red','size'=>'big','price'=>16);//Declaration of a mixed data array with index key.
?>
Consider the array declared by the following statement.
$myvariable=array(4,5,'three',2,5);
Here $myvariable contain five data which are $myvariable[0], $myvariable[1], ....,$myvariable[4]. The integers in the square bracket are the index number of an array. Normally index numbers are started from 0 and ended with (elements count - 1).
Instead of index number you can also specify an array element by its index key. Consider the following array declaration,
$myvariable=array('color'=>'red','size'=>'big','price'=>16);
Here $myvariable['color'] is 'red' and $myvariable['price'] is 16. Here 'color' and 'price' are the index key of $myvariable[0] and $myvariable[2].
When the elements of an array are associated with a unique index key the array is called an associative array. A simple example of associative array is array post variables or array get variables. You often see this type of code when you retrieve form submitted data:
$name=$_POST['name']; or $name=$_GET['name'];
Here $_POST and $_GET are two global associative array variables used in PHP.
Please note that if you specify array key you will be still able to access an individual array element with its numeric index number.
In the above examples we learn how to access each individual element in an array in PHP. Now we shall learn how to access and process every element in an array by using foreach loop.
Example of array access by foreach loop:
<?php
$myvariable=array(4,5,'three',2,5);
foreach($myvariable as $element)
{
echo $element.'<br />;
}
/*
Output:
4
5
three
2
5
*/
?>
The function count() is one of the most useful function in handling array. This function always returns the number of elements (variables) in an array. If you execute the following command
$elements_count = count($myvariable);
the variable $elements_count will be assigned to a value of 5.
<?php
$myvariable=array(4,5,'three',2,5);
for($i=0; $i<count($myvariable); $i++)
{
echo $ myvariable[$i].'<br />';
}
/*
Output:
4
5
three
2
5
*/
?>
In the following example we shall learn how to add new data in PHP array.
<?php
$array=array(); //Declaration of an empty array.
$array[]='name';
$array[]='address';
$array['age']=26;
foreach($myvariable as $element){echo $element.'<br />';}
/* Output:
name
address
26
*/
?>
Use PHP unset() function to delete a data from PHP array. Consider the following example.
<?php
$array=array(); //Declaration of an empty array.
$array[]='name';
$array[]='address';
$array['age']=26;
foreach($myvariable as $element){echo $element.'<br />';}
unset $array[1];
echo 'After delete 2nd element<br />';
foreach($myvariable as $element){echo $element.'<br />';}
/* Output:
name
address
26
After delete 2nd element
Name
26
*/
?>