PHP Arrays are used to associate values to keys; ordered maps. It is a special type of variable that can hold multiple values.
For instance if you have a list of beers and you wanted to manipulate and sort this data, you would need to assign them variables in PHP.
If you did this with standard variables it would look like this:
$beer1=”guiness”;
$beer2=”chimaye”;
$beer3=”miller”;
If you had 100 beers and you wanted to scroll through them all, this could be quite a hassle.
With a numeric array in PHP you could simply say:
$beer=array(“Guiness”, “Chimaye”, “Miller”);
More simply, arrays assign a value, starting at 0 to each.
So, $array = array(1, 2, 3, 4, 5); print_r(array); would yield:
array (
[0] = 1
[1] = 2
[2] = 3
[3] = 4
[4] = 5
[5] = 6
)
Also important to note. String keys should be placed in single quotes if they are a literal array index key but Do not quote keys which are constants or variables, as this will prevent PHP from interpreting them.
<h2>3 types of arrays</h2>
There are three types of arrays. Our example above was a numeric array where index values where automatically assigned (starting at 0)
1. Numeric Arrays: Arrays with a numeric index.
2. Associative Arrays: An array where each ID key is associated with a value.
3. Multidimensional Arrays: An array containing one or more arrays.