What is an Array?
An array is an extraordinary variable, that can store more than one value or a set of values at a time.
It provides fewer code characteristics because we can specify a single variable instead of multiple variables. The array also defines sorting features.
Example :
In this example we can hold multiple value in multiple variables.
<!DOCTYPE html> <html> <body> <?php // This is a multiple variable $colors1 = "Red"; $colors2 = "Pink"; $colors3 = "Yellow"; echo "My favorite colors is " . $colors1 . ", " . $colors2 . " and " . $colors3 . "."; ?> </body> </html>
Output
Example :
In this example we can hold multiple value in single variable.
<!DOCTYPE html> <html> <body> <?php // This is a single variable $colors = array("Red", "Pink", "Yellow"); echo "MY favorite colors is " . $colors[0] . ", " . $colors[1] . " and " . $colors[2] . "."; ?> </body> </html>
Output
PHP Array Types
There is three distinct types of arrays which is given below:
- Indexed array
- Associative array
- Multidimensional array
PHP Indexed array
An array with a numeric index that begins from 0. In an indexed array, each element’s values are kept and accessed in a linear fashion.
There are two ways to define an indexed array, the simplest way is:
Example: 1st way
<?php $colors=array("Red","Pink","Yellow"); ?>
Example: 2nd way
<?php $colors[0] = "Red"; $colors[1] = "Pink"; $colors[2] = "Yellow"; ?>
PHP Associative Arrays
The associative arrays are very equivalent to numeric arrays in terms of functionality. But they are different in the words of their index. Associative array will have their index as a string so that you can specify a strong connection between keys and values.
For example, if you want to accumulate the salary of employees in an array, a numerically indexed array would not be the best choice.
Rather, we could use the employees’ names as the keys in our associative array, and the value would be their respective salaries.
NOTE: Don’t keep associative array inside double quote while printing otheriwse it would not return any value.
Example
<html> <body> <?php /* First method to associate create array. */ $salary = array("John" => 2200, "Carrillo" => 1200, "James" => 700); echo "Salary of John is ". $salary['John'] . "<br />"; echo "Salary of Carrillo is ". $salary['Carrillo']. "<br />"; echo "Salary of James is ". $salary['James']. "<br /><br />"; /* Second method to create array. */ $salary['John'] = "high"; $salary['Carrillo'] = "medium"; $salary['James'] = "low"; echo "John's salary is ". $salary['John'] . "<br />"; echo "Carrillo's salary is ". $salary['Carrillo']. "<br />"; echo "James' salary is ". $salary['James']. "<br />"; ?> </body> </html>
Output
The result will here −
Salary of Carrillo is 1200
Salary of James is 700
John’s salary is high
Carrillo’s salary is medium
James’ salary is low
PHP – Multidimensional Arrays
In a multidimensional array, the main array can also contain one or more arrays that are two, three, four, five, or more levels deep.