The individual elements in an array can be accessed using the array variable’s name, and the location index surrounded by square brackets (
), for example:
$my_array = ["tic", "tac", "toe"]; echo $my_array; // Prints: tac
This process is sometimes referred to as indexing an array.
Remember the computer evaluates variables it encounters (outside of assignment): it replaces them with the values they hold. Let’s look at an example of indexing an array with a number variable:
$num_var = 2; $important_info = ["talking chicken", 181, "magnets?!", 99]; echo $important_info[$num_var]; // Prints: magnets?!
Let’s practice accessing elements in arrays!
Create a new array
$winners. This array should have three elements—the winning element from each of the three provided arrays. Use array indexing to access the elements.
Use any method you’d like to print the contents of the