In indexed arrays, each item has an index number.
By default, the first item has an index of 0, the second item has an index of 1, and so on.
Create and output an indexed array:
$cars |
To access an array item, you can use its index number.
Output the first item of the array:
$cars |
To modify the value of an array item, use its index number:
Update the value of the second item:
$cars |
To iterate through and display all the values of an indexed array, you can use a foreach loop, as follows:
Output all items in the array:
$cars |
The key of an indexed array is a number, with the first item being 0, the second 1, and so on, although there are exceptions.
New items receive the next index number, which is one greater than the highest existing index.
For example, if you have an array like this:
$cars[0] |
If you use the array_push() function to add a new item, the new item will be assigned the index 3.
array_push($cars, |
However, if you have an array with random index numbers, like this:
$cars[5] |
If you use the array_push() function to add a new item, what will be its index number?
array_push($cars, |