PHP Arrays
An array variable is a collection of many values under a single variable name. We don't need to define multiple variables to store multiple values. An array can hold many values under a single name and we can access the values by referring to an index number.
Type of Arrays- Indexed or Numeric arrays
- Associative arrays
- Multidimensional arrays
Indexed or Numeric Arrays
An indexed or numeric array stores each array element with a numeric index which starts from 0. PHP indexed arrays can store numbers, strings or objects.
Use // or # at the beginning of a line.
Eg:
<?php
$fruits = array("apple","orange","banana");
?>
Here indices are automatically assigned. We can assign the indices manually also, as given below:
<?php
$fruits[0] = "apple";
$fruits[1] = "orange";
$fruits[2] = "banana";
?>
Associative Arrays
In an associative array, the keys assigned to values can be arbitrary and user defined strings. We can easily remember the element because each element is represented by a label rather than an index number.
There are two ways to create an associative array:
Eg:
<?php
$fruit_weight = array("apple"=>"200", "orange"=>"150", "banana"=>"300");
// OR
$fruit_weight["apple"] = "200";
$fruit_weight["orange"] = "150";
$fruit_weight["banana"] = "300";
echo "Banana weighs ".$fruit_weight["banana"]." grams.";
?>
We can loop through an associative array using foreach.
Eg:
<?php
$fruit_weight = array("apple"=>"200", "orange"=>"150", "banana"=>"300");
foreach($fruit_weight as $key => $weight_value)
{
echo $key." weighs ".$weight_value." grams.";
echo "<br>";
}
?>
Output will be
apple weighs 200 grams.
orange weighs 150 grams.
banana weighs 300 grams.
Multidimensional Arrays
A multidimensional array is an array which contains one or more arrays. They store another array at each index instead of a single element. We can use two dimensional arrays, three dimensional arrays and more. For each level added, arrays become more complicated. If we have an n-dimensional array, we need an 'n' number of indices to select an element. Default key for each element will start from 0.
A PHP two dimensional array is an array of arrays while a three dimensional array is an array of (array of arrays).
PHP two dimensional array Eg:
Cricketer | Centuries | Half-centuries |
---|---|---|
Sachin | 49 | 96 |
Ganguly | 22 | 72 |
<?php
$cricketers = array(
"sachin" => array (
"centuries" => 49,
"half_centuries" => 96
),
"ganguly" => array (
"centuries" => 22,
"half_centuries" => 72
)
);
echo "Centuries for Sachin in ODIs : " ;
echo $cricketers['sachin']['centuries'] . "<br>"; //output 49
echo "Half centuries for Ganguly in ODIs : ";
echo $cricketers['ganguly']['half_centuries'] . "<br>"; // output 72
?>
Output:
Centuries for Sachin in ODIs : 49
Half centuries for Ganguly in ODIs : 72
Using foreach loop in a two dimensional array
<?php
foreach($cricketers as $key=>$cricketer)
{
echo '<h3>'.ucfirst($key)."</h3>";
echo "Centuries: ".$cricketer["centuries"]."<br/>";
echo "Half-centuries: ".$cricketer['half_centuries']."<br/>";
}
?>
Output:
Sachin
Centuries: 49Half-centuries: 96
Ganguly
Centuries: 22Half-centuries: 72