To access an array item, use the index number for indexed arrays and the key name for associative arrays.
Access an item by using its index number:
$cars |
Note: The first item has an index of 0. |
To access items in an associative array, use the key name:
Access an item by using its key name:
$cars |
You can use either double or single quotes when accessing an array:
echo |
Array items can be of any data type, including functions.
To execute a function stored in an array, use the index number followed by parentheses ( ):
Call a function item:
function $myArr |
Use the key name when the function is an item in an associative array:
Call a function by referring to its key name:
function $myArr $myArr["message"](); |
To iterate through and print all the values of an associative array, you can use a foreach loop like this:
Print all array items with their keys and values:
$car foreach |
To iterate through and print all the values of an indexed array, you can use a foreach loop like this:
Print all array items:
$cars |