Recall & Review
beginner
What does the PHP function
array_unique() do?It removes duplicate values from an array, keeping only the first occurrence of each value.
Click to reveal answer
beginner
What is the purpose of the PHP function
array_flip()?It swaps the keys and values of an array, turning values into keys and keys into values.
Click to reveal answer
beginner
Given
$arr = ['apple', 'banana', 'apple', 'orange'];, what is the result of array_unique($arr);?The result is
['apple', 'banana', 'orange'] with keys preserved as [0 => 'apple', 1 => 'banana', 3 => 'orange'].Click to reveal answer
intermediate
What happens if you use
array_flip() on an array with duplicate values?Only the last key for each value is preserved because keys must be unique. Earlier keys are overwritten.
Click to reveal answer
intermediate
How can you combine
array_unique() and array_flip() to get a flipped array with unique values as keys?First use
array_unique() to remove duplicates, then use array_flip() to swap keys and values, ensuring keys are unique.Click to reveal answer
What does
array_unique() return when given an array with duplicate values?✗ Incorrect
array_unique() removes duplicates and keeps the first occurrence's key.
What will
array_flip(['a' => 1, 'b' => 2, 'c' => 1]) return?✗ Incorrect
When flipping, duplicate values become keys; only the last key is kept.
Which function would you use to remove duplicate values from an array in PHP?
✗ Incorrect
array_unique() removes duplicate values.
If you want to swap keys and values of an array, which function do you use?
✗ Incorrect
array_flip() swaps keys and values.
What is a potential problem when using
array_flip() on an array with non-unique values?✗ Incorrect
Duplicate values become keys, but keys must be unique, so only the last key remains.
Explain how
array_unique() and array_flip() work and how they can be used together.Think about removing duplicates first, then swapping keys and values.
You got /4 concepts.
What happens if you flip an array with duplicate values? How does PHP handle the keys?
Consider how keys in PHP arrays must be unique.
You got /4 concepts.