In the process of developing using PHP, sooner or later, you will need to create many similar variables.
Without a lot of similar variables, you can store the data as an element in an array.
The elements in an array have their own IDs, so you can easily access them.
There are three types of arrays:
Numeric array
An array with a numeric ID key
Example
$names = Array ("Peter", "Quagmire", "Joe");
echo $names [1]. "and". $names [2]. "Are". $names [0]. "' s Neighbors";
The output of the above code:
Quagmire and Joe are Peter ' s neighbors
Associative arrays
Each ID key in the array associates a value
$ages = Array ("Peter" =>32, "quagmire" =>30, "Joe" =>34);
echo "Peter is". $ages [' Peter ']. "Years old."
Output from the above script:
Peter is years old.
Multidimensional arrays
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 something like this:
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)