PHP Arrays
There are three different kinds of arrays:
- Numeric array—An array with a numeric ID key
- Associative array—An array where each ID is associated with a value
- Multidimensional array—An array containing one or more arrays
Numeric Arrays
|
|
$names = array( "Peter",
"Quagmire", "Joe" );
|
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
|
A numeric array stores each element with a numeric ID key.
There are different ways to create a numeric array.
In the above examples, the Example I is the same as the Example II.
|
|
|
Associative Arrays
When storing data about specific named values, a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assign values to them.
In the following examples, the Example III is the same as the Example IV.
|
|
$ages = array( "Peter" => 32,
"Quagmire" => 30, "Joe" => 34 );
|
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
|
Boy: Wanna hear a joke?
Girl: No thanks. I’m already looking at one.
|