In the process of developing with PHP, sooner or later, you will need to create many similar variables.
Instead of having many similar variables, you can store data as elements in an array.
The elements in the array have their own IDs, so they can be accessed easily.
There are three array types:
Numeric array
Array with numeric ID keys
Example
$names = array("Peter","Quagmire","Joe");
echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors";
Output of the above code:
Quagmire and Joe are Peter's neighbors
Associative array
Each ID key in the array is associated with a value
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
echo "Peter is " . $ages['Peter'] . " years old.";
Output of the above script:
Peter is 32 years old.
Multidimensional array
An array containing one or more arrays
$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
If you output this array, it should look like this:
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)