Numeric array

Numeric arrays are where the keys are numbers. It always starts at 0 therefore the key of the first element is 0, the next is 1 and so no. I know that’s pretty confusing and you’re probably thinking “why can’t it start at 1?”. That’s just the way it is in the programming world. You’ll get use to it eventually.

We declare the array like below:
[php]
$yummies = array("icecream", "sweets", "cupcakes");
[/php]

If we print this out, it will produce:

Array = (
[0] = “icecream”,
[1] = “sweets”,
[2] = “cupcakes”
)

Numeric array will automatically have the keys in an ordered list. We can print out each individual value as well as printing the whole array.

[php]
echo $yummies[0];
[/php]
That will output:

icecream

[php]
echo $yummies[1];
[/php]
That will output:

sweets

You can join up elements in an array to form some text, this is similar to the joining of String Variables.
[php]
echo "I love ".$yummies[0]." and ".$yummies[1]." very much";
[/php]
The output will be:

I love icecream and sweets very much

Adding to an array
To add another value to the end of your array, you can simply declare a blank key with the new value.
[php]
$yummies = array("icecream", "sweets", "cupcakes");

$yummies[] = "chocolate";
[/php]

The example above will produce an array like below:

Array = (
[0] = “icecream”,
[1] = “sweets”,
[2] = “cupcakes”,
[3] = “chocolate”
)