0
0
PHPprogramming~10 mins

Array key and value extraction in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Avalues
Bflip
Ckeys
Dmerge
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_values instead of array_keys.
Trying to use array_flip which swaps keys and values.
2fill in blank
medium

Complete 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'
Avalues
Bflip
Ckeys
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_keys instead of array_values.
Using array_pop which removes the last element.
3fill in blank
hard

Fix 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'
Aflip
Breverse
Ckeys
Dvalues
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.
4fill in blank
hard

Fill 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'
Akeys
Bvalues
Cflip
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up keys and values functions.
Using array_flip which swaps keys and values instead of extracting them.
5fill in blank
hard

Fill 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'
Aflip
Bvalues
Ckeys
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_pop which removes elements instead of extracting keys or values.
Mixing up keys and values order.