Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get all the keys from the array.
PHP
<?php $fruits = ['a' => 'Apple', 'b' => 'Banana', 'c' => 'Cherry']; $keys = array_[1]($fruits); print_r($keys); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_values instead of array_keys.
Trying to use array_flip which swaps keys and values.
✗ Incorrect
The array_keys function returns all the keys from an array.
2fill in blank
mediumComplete the code to get all the values from the array.
PHP
<?php $colors = ['red' => '#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF']; $values = array_[1]($colors); print_r($values); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_keys instead of array_values.
Using array_pop which removes the last element.
✗ Incorrect
The array_values function returns all the values from an array.
3fill in blank
hardFix the error in the code to swap keys and values of the array.
PHP
<?php $items = ['pen' => 'blue', 'book' => 'red', 'bag' => 'black']; $flipped = array_[1]($items); print_r($flipped); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_keys or array_values which do not swap keys and values.
Using array_reverse which reverses the order but does not swap keys and values.
✗ Incorrect
The array_flip function swaps the keys and values of an array.
4fill in blank
hardFill both blanks to create an array of keys and values from the original array.
PHP
<?php $person = ['name' => 'Alice', 'age' => 30, 'city' => 'Paris']; $keys = array_[1]($person); $values = array_[2]($person); print_r($keys); print_r($values); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up keys and values functions.
Using array_flip which swaps keys and values instead of extracting them.
✗ Incorrect
Use array_keys to get keys and array_values to get values.
5fill in blank
hardFill all three blanks to create a flipped array and then get its keys and values.
PHP
<?php $colors = ['red' => '#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF']; $flipped = array_[1]($colors); $flipped_keys = array_[2]($flipped); $flipped_values = array_[3]($flipped); print_r($flipped_keys); print_r($flipped_values); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_pop which removes elements instead of extracting keys or values.
Mixing up keys and values order.
✗ Incorrect
First flip the array, then get keys and values from the flipped array.