Complete the code to access the first element of the array.
<?php $fruits = ['apple', 'banana', 'cherry']; echo $fruits[[1]]; ?>
The first element in a PHP array starts at index 0.
Complete the code to change the second element of the array to 'orange'.
<?php $colors = ['red', 'green', 'blue']; $colors[[1]] = 'orange'; print_r($colors); ?>
The second element has index 1, so we assign 'orange' to $colors[1].
Fix the error in the code to correctly add a new element 'pear' to the array.
<?php $fruits = ['apple', 'banana']; $fruits[1] = 'pear'; print_r($fruits); ?>
Using empty square brackets [] adds a new element at the next available index.
Fill both blanks to create an associative array with keys as names and values as ages.
<?php $ages = [[1] => 25, [2] => 30]; print_r($ages); ?>
Keys in associative arrays are strings. Here, 'Alice' and 'Bob' are used as keys.
Fill all three blanks to create an array of squares for numbers greater than 3.
<?php $numbers = [1, 2, 3, 4, 5]; $squares = [[1] => [2] for [3] in $numbers if [3] > 3]; print_r($squares); ?>
We use $number as the variable name and square it with $number * $number for numbers greater than 3.