0
0
PHPprogramming~5 mins

Array unique and flip in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
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
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
Which function would you use to remove duplicate values from an array in PHP?
Aarray_flip()
Barray_unique()
Carray_merge()
Darray_slice()
If you want to swap keys and values of an array, which function do you use?
Aarray_unique()
Barray_keys()
Carray_flip()
Darray_reverse()
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
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.