Learn
Ordered Arrays
Accessing an Element

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[1]; // 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!

Instructions

1.

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.

2.

Use any method you’d like to print the contents of the $winners array.

Folder Icon

Take this course for free

Already have an account?