The array variable is used to store a list of data. Different types of multiple data can be stored in an array variable. Every value of the array can be read by the corresponding array index. Three types of array variables can be declared in PHP. These are numeric array, associative array, and multidimensional array.
Multiple examples have shown the uses of these arrays in this tutorial.
Numeric Array
The array that contains numeric index only is called a numeric array. You can declare a numeric array in various ways, such as defining array structure, using the array() method, and assigning values. Different types of numeric array declaration are provided in the following example.
Example 1: Different Types of Numeric Array Declarations
Create a PHP file with the following script to discover the uses of the numeric array. Here, the foreach loop has been used to traverse each element of the array. The first array has been declared using the third bracket ([]). Next, the second array has been declared using the array() function. Finally, the third array has been declared by initializing each value separately. These declarations can be used to create the one-dimensional numeric array in PHP.
/*Define array using array structure */
$array1 = ['Red', 'Green', 'Blue'];
echo '<br/>The values of array1: <br/>';
foreach( $array1 as $value ) {
echo "$value <br />";
}
/*Define array using array method */
$array2 = array('Rose','Lily','Sun Flower','China Rose');
echo '<br/>The values of array2: <br/>';
foreach( $array2 as $value ) {
echo "$value <br />";
}
/* Define array using index */
$array3[0] = "Mango";
$array3[1] = "Grape";
$array3[2] = "Banana";
$array3[3] = "Guava";
$array3[4] = "Orange";
echo '<br/>The values of array3: <br/>';
foreach($array3 as $value ) {
echo "$value <br />";
}
?>
Now, open the following browser and browse the location to run the script:
http://localhost/phpcode/numArray.php
Output:
The following output will appear after executing the previous script:
Associative Array
The declaration of an associated array is similar to a numeric array. You can declare a numeric array without defining the index but you can’t declare an associative array without defining the index. You must define key-value pairs properly at the time of associative array declaration and key or index value must be a string.
Example 2: Associative Array of a Numeric Index
Create a PHP file with the following script. Here, an associative array of 5 elements with the numeric index has been declared, and the “foreach” loop has been used to print the array values.
//Declare an associative array of numeric index
$depts = array(1 => "CSE", 2 => "ETE", 3 => "BBA",
4 => "English", 5 =>"Pharmacy");
echo "<b>The department names are:</b><br/>";
//Print the array values using loop
foreach($depts as $dept)
{
echo "$dept<br/>";
}
?>
Output:
The following output will appear after executing the previous script:
Example 3: Associative Array of String Index
Create a PHP file with the following script to show the use of an associative array with the string as the index value. Two ways of associative array declarations have been displayed in the following script. The first associative array has been declared using the array() function, and the “foreach” loop has been used to print the array values. The second associative array has been declared by assigning each value separately and printing the values separately.
/* Associate array declaration using array() method */
$Books = array("Beginning PHP and MySQL" => 100, "Learning JQuery" => 60, "Laravel 5.5" => 150,"AngularJS" => 160);
foreach( $Books as $key=>$value ) {
echo "The price of <b> $key </b> is <b> $value </b><br />";
}
/* Associate array declaration by assigning values */
$Person['John'] = "White";
$Person['Peter'] = "Yellow";
$Person['Ella'] = "pink";
$Person['Fahmida'] = "Blue";
$Person['Mick'] = "Red";
echo "<br/>The favorite color of <b> John </b> is: <b>". $Person['John'] . "</b><br />";
echo "The favorite color of <b> Peter </b> is: <b>". $Person['Peter']. "</b><br />";
echo "The favorite color of <b> Fahmida </b> is: <b>". $Person['Fahmida']. "<b><br />";
?>
Output:
The following output will appear after executing the provided script above:
Multidimensional Array
One or more arrays can be used as the array element of the multidimensional array, and the element of the array can also contain other array(s) as the element. The multiple indexes or for loop can be used to access the values of a multidimensional array. The way to create and use a multidimensional array in PHP has been provided in the following examples:
Example 4: Two-Dimensional Associative Array of Numeric Values
Create a PHP file with the following script to know how to create a multidimensional array of numeric values and print the values of the array by using a nested “foreach” loop.
//Declare a multidimentional associative array of numeric values
$items = array(
"Cake" => array(
"Vanilla Cake" => 15,
"Chocolate Cake" => 25
),
"Pizza" => array(
"Chicken Pizza" => 20,
"Beef Pizza" => 35,
"Vegetable Pizza" => 15
),
"Ice Cream" => array(
"strawberry Flavour" => 30,
"Vanilla Flavour" => 22,
"Chocolate Flavour" => 26,
"Mango Flavour" => 30,
),
);
//Print the multidimentional array using loop
foreach($items as $key=>$value)
{
echo "<br/><b>The list of '$key' items are:</b><br/>";
foreach($value as $k=>$v)
{
echo "$k - $$v<br/>";
}
}
?>
Output:
The following output will appear after executing the previous script:
Example 5: Two-Dimensional Associative Array of String Values
Create a new PHP file with the following script to know how to create a multidimensional array of string values and print the values of the array by using a nested “foreach” loop. The way of printing the array values with the keys is shown in the last part of the script.
/*Multidimentional array declaration using array() method*/
$employees = array(
"Jonny" => array("post" => "Sales Executive","email" =>"[email protected]","phone" => "953456788"),
"Mac" => array("post" => "Manager","email" =>"[email protected]","phone" => "900267748"),
"Gilmore" => array("post" => "Director","email" =>"[email protected]","phone" => "988777789"), );
/*Reading multidimensional array using for loop */
foreach( $employees as $key=>$value ) {
echo "<br/>";
echo "Employee Name:<b> $key </b><br/>";
foreach( $value as $k=>$v ) {
echo "$k: <b> $v </b><br/>";
}
}
echo "<br/>";
/* Reading multidimensional array multiple indexes */
echo "The email address of <b> Jonny </b> is : " ;
echo $employees['Jonny']['email'] . "<br />";
echo "The phone number of <b> Mac </b> is : ";
echo $employees['Mac']['phone'] . "<br />";
echo "The designation of <b> Gilmore </b> is : " ;
echo $employees['Gilmore']['post'] . "<br />";
?>
Output:
The following output will appear after executing the previous script:
Conclusion:
Different arrays are used in the PHP script for solving various problems. The ways of using the numeric array, associative array, and multidimensional arrays have been explained in this tutorial. Plus, examples were provided to understand the uses of the array in PHP and help the PHP user to use it properly in their script. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.