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?
AAn array sorted in ascending order
BAn array with duplicates removed, preserving the last occurrence keys
CAn array with all values replaced by keys
DAn array with duplicates removed, preserving the first occurrence keys
✗ Incorrect
array_unique() removes duplicates and keeps the first occurrence's key.
What will array_flip(['a' => 1, 'b' => 2, 'c' => 1]) return?
AArray with keys and values swapped, last duplicate key kept
BArray with keys and values swapped, first duplicate key kept
COriginal array unchanged
DError because of duplicate values
✗ 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?
Aarray_flip()
Barray_unique()
Carray_merge()
Darray_slice()
✗ Incorrect
array_unique() removes duplicate values.
If you want to swap keys and values of an array, which function do you use?
Aarray_unique()
Barray_keys()
Carray_flip()
Darray_reverse()
✗ Incorrect
array_flip() swaps keys and values.
What is a potential problem when using array_flip() on an array with non-unique values?
AIt keeps only the last key for duplicate values
BIt duplicates keys
CIt causes a syntax error
DIt sorts the array
✗ 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.
Practice
(1/5)
1. What does the PHP function array_unique() do to an array?
easy
A. Flips keys and values of the array
B. Removes duplicate values, keeping only the first occurrence
C. Sorts the array in ascending order
D. Removes all values except the last occurrence
Solution
Step 1: Understand the purpose of array_unique()
This function scans the array and removes any duplicate values, but keeps the first time each value appears.
Step 2: Compare with other options
Options B, C, and D describe different functions or behaviors. Only Removes duplicate values, keeping only the first occurrence correctly describes array_unique().
Final Answer:
Removes duplicate values, keeping only the first occurrence -> Option B
Quick Check:
array_unique() removes duplicates = A [OK]
Hint: Remember: unique means no duplicates, keep first [OK]
Common Mistakes:
Thinking it flips keys and values
Assuming it sorts the array
Believing it keeps the last occurrence
2. Which of the following is the correct syntax to flip keys and values of an array $arr in PHP?
easy
A. array_flip($arr);
B. flip_array($arr);
C. array_unique($arr);
D. flip($arr);
Solution
Step 1: Identify the correct function name
The PHP function to swap keys and values is array_flip(). Other options are not valid PHP functions.
Step 2: Confirm correct usage
The syntax is array_flip($arr); which returns a new array with keys and values swapped.
You want to create a new array where usernames are keys and user IDs are values, but only keep the first occurrence of each username. Which code correctly achieves this?
hard
A. $uniqueUsers = array_flip($users); $uniqueUsers = array_unique($uniqueUsers);
B. $uniqueUsers = array_unique(array_flip($users));
C. $uniqueUsers = array_flip(array_unique($users));
D. $uniqueUsers = array_unique($users); $uniqueUsers = array_unique($uniqueUsers);
Solution
Step 1: Remove duplicate usernames with array_unique()
This keeps the first occurrence of each username, removing duplicates.
Step 2: Flip keys and values with array_flip()
Swaps usernames to keys and user IDs to values, as required.
Final Answer:
$uniqueUsers = array_flip(array_unique($users)); -> Option C
Quick Check:
Unique usernames first, then flip keys and values [OK]
Hint: Unique first, then flip for correct key-value swap [OK]
Common Mistakes:
Flipping before removing duplicates
Applying unique after flipping
Assuming array_unique works on keys
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.