Bird
Raised Fist0
PHPprogramming~20 mins

Array unique and flip in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Unique and Flip Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of array_unique and array_flip combination
What is the output of the following PHP code?
PHP
<?php
$arr = ['apple', 'banana', 'apple', 'orange', 'banana'];
$result = array_flip(array_unique($arr));
print_r($result);
?>
AArray ( [apple] => 0 [banana] => 1 [orange] => 3 )
BArray ( [0] => apple [1] => banana [3] => orange )
CArray ( [apple] => 2 [banana] => 4 [orange] => 3 )
DArray ( [apple] => 0 [banana] => 2 [orange] => 3 )
Attempts:
2 left
💡 Hint
Remember that array_unique keeps the first occurrence of each value and array_flip swaps keys and values.
❓ Predict Output
intermediate
1:30remaining
Count of items after array_unique and array_flip
How many items are in the resulting array after running this code?
PHP
<?php
$input = ['cat', 'dog', 'cat', 'bird', 'dog', 'fish'];
$output = array_flip(array_unique($input));
echo count($output);
?>
A6
B4
C3
D5
Attempts:
2 left
💡 Hint
Count unique values before flipping.
🔧 Debug
advanced
2:00remaining
Identify the error in array_flip usage
What error will this PHP code produce?
PHP
<?php
$arr = ['a' => 'x', 'b' => 'x', 'c' => 'y'];
$result = array_flip($arr);
print_r($result);
?>
AArray ( [x] => c [y] => c )
BArray ( [x] => b [y] => c )
CWarning: array_flip(): Can only flip STRING and INTEGER values!
DArray ( [x] => b [y] => c ) but keys overwritten silently
Attempts:
2 left
💡 Hint
Check what happens when array_flip has duplicate values in the original array.
📝 Syntax
advanced
1:00remaining
Syntax error in array_unique usage
Which option contains a syntax error when trying to get unique values from an array?
A$unique = array_unique arr;
B$unique = array_unique($arr);
C$unique = array_unique($arr, SORT_STRING);
D$unique = array_unique($arr, SORT_REGULAR);
Attempts:
2 left
💡 Hint
Check the function call syntax carefully.
🚀 Application
expert
2:30remaining
Result of combined array_unique and array_flip with numeric keys
Given the code below, what is the output?
PHP
<?php
$input = [2 => 'red', 5 => 'blue', 3 => 'red', 7 => 'green'];
$output = array_flip(array_unique($input));
print_r($output);
?>
AArray ( [red] => 0 [blue] => 1 [green] => 2 )
BArray ( [red] => 3 [blue] => 5 [green] => 7 )
CArray ( [red] => 2 [blue] => 5 [green] => 7 )
DArray ( [2] => red [5] => blue [7] => green )
Attempts:
2 left
💡 Hint
array_unique keeps the first key for each unique value. array_flip swaps keys and values.

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

  1. 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.
  2. 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().
  3. Final Answer:

    Removes duplicate values, keeping only the first occurrence -> Option B
  4. 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

  1. 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.
  2. Step 2: Confirm correct usage

    The syntax is array_flip($arr); which returns a new array with keys and values swapped.
  3. Final Answer:

    array_flip($arr); -> Option A
  4. Quick Check:

    Flip keys and values = array_flip() [OK]
Hint: Flip keys and values with array_flip() [OK]
Common Mistakes:
  • Using non-existent functions like flip_array()
  • Confusing array_unique() with array_flip()
  • Missing parentheses in function call
3. What is the output of the following PHP code?
$arr = ['a' => 'apple', 'b' => 'banana', 'c' => 'apple'];
$result = array_flip(array_unique($arr));
print_r($result);
medium
A. Array ( [apple] => a [banana] => b )
B. Array ( [a] => apple [b] => banana [c] => apple )
C. Array ( [apple] => c [banana] => b )
D. Array ( [apple] => 0 [banana] => 1 )

Solution

  1. Step 1: Apply array_unique() on $arr

    This removes duplicate values, so 'apple' at key 'c' is removed. Result: ['a' => 'apple', 'b' => 'banana']
  2. Step 2: Apply array_flip() on the unique array

    Keys and values swap: 'apple' becomes key with value 'a', 'banana' becomes key with value 'b'.
  3. Final Answer:

    Array ( [apple] => a [banana] => b ) -> Option A
  4. Quick Check:

    Unique then flip = keys and values swapped without duplicates [OK]
Hint: Unique first, then flip keys and values [OK]
Common Mistakes:
  • Flipping before removing duplicates
  • Expecting duplicate keys in flipped array
  • Confusing keys and values in output
4. Consider this PHP code snippet:
$arr = ['x' => 'dog', 'y' => 'cat', 'z' => 'dog'];
$result = array_flip($arr);
print_r($result);
What is the problem with this code?
medium
A. No problem; output will be correct
B. It causes a syntax error because array_flip requires numeric keys
C. array_flip cannot flip string keys
D. The duplicate values cause keys to be overwritten in the flipped array

Solution

  1. Step 1: Understand array_flip() behavior with duplicates

    When flipping, values become keys. Duplicate values cause keys to overwrite previous ones.
  2. Step 2: Analyze the example

    'dog' appears twice as a value. After flipping, only the last key ('z') remains for 'dog'.
  3. Final Answer:

    The duplicate values cause keys to be overwritten in the flipped array -> Option D
  4. Quick Check:

    Duplicate values overwrite keys when flipped [OK]
Hint: Duplicates in values overwrite keys after flip [OK]
Common Mistakes:
  • Expecting all keys to be preserved
  • Thinking array_flip causes syntax errors
  • Assuming string keys cannot be flipped
5. You have an array of user IDs with possible duplicates:
$users = [101 => 'alice', 102 => 'bob', 103 => 'alice', 104 => 'carol'];
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

  1. Step 1: Remove duplicate usernames with array_unique()

    This keeps the first occurrence of each username, removing duplicates.
  2. Step 2: Flip keys and values with array_flip()

    Swaps usernames to keys and user IDs to values, as required.
  3. Final Answer:

    $uniqueUsers = array_flip(array_unique($users)); -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard