We use array unique and flip to remove duplicate values and swap keys with values in an array. This helps organize data and find unique items easily.
Array unique and flip in PHP
<?php // Remove duplicates from an array $uniqueArray = array_unique($array); // Flip keys and values in an array $flippedArray = array_flip($array); ?>
array_unique() keeps the first occurrence and removes later duplicates.
array_flip() swaps keys and values; values must be unique and valid keys (strings or integers).
<?php $array = ['apple', 'banana', 'apple', 'orange']; $uniqueArray = array_unique($array); print_r($uniqueArray); ?>
<?php $array = ['a' => 'red', 'b' => 'green', 'c' => 'red']; $flippedArray = array_flip($array); print_r($flippedArray); ?>
<?php $array = []; $uniqueArray = array_unique($array); print_r($uniqueArray); ?>
<?php $array = ['onlyone']; $flippedArray = array_flip($array); print_r($flippedArray); ?>
This program shows how to remove duplicates from an array and then flip keys and values. It prints the array before and after each step.
<?php // Original array with duplicates $fruits = ['apple', 'banana', 'apple', 'orange', 'banana']; // Print original array echo "Original array:\n"; print_r($fruits); // Remove duplicates $uniqueFruits = array_unique($fruits); echo "\nArray after removing duplicates:\n"; print_r($uniqueFruits); // Flip keys and values $flippedFruits = array_flip($uniqueFruits); echo "\nArray after flipping keys and values:\n"; print_r($flippedFruits); ?>
Time complexity: array_unique() and array_flip() both run in O(n) time, where n is the number of elements.
Space complexity: Both functions create new arrays, so space is O(n).
Common mistake: Using array_flip() on arrays with duplicate values causes keys to be overwritten silently.
Use array_unique() when you want to keep only one copy of each value. Use array_flip() when you want to quickly find keys by their values.
array_unique() removes duplicate values, keeping the first occurrence.
array_flip() swaps keys and values, but values must be unique and valid keys.
These functions help organize and search data efficiently in arrays.
Practice
array_unique() do to an array?Solution
Step 1: Understand the purpose of
This function scans the array and removes any duplicate values, but keeps the first time each value appears.array_unique()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 describesarray_unique().Final Answer:
Removes duplicate values, keeping only the first occurrence -> Option BQuick Check:
array_unique() removes duplicates = A [OK]
- Thinking it flips keys and values
- Assuming it sorts the array
- Believing it keeps the last occurrence
$arr in PHP?Solution
Step 1: Identify the correct function name
The PHP function to swap keys and values isarray_flip(). Other options are not valid PHP functions.Step 2: Confirm correct usage
The syntax isarray_flip($arr);which returns a new array with keys and values swapped.Final Answer:
array_flip($arr); -> Option AQuick Check:
Flip keys and values = array_flip() [OK]
- Using non-existent functions like flip_array()
- Confusing array_unique() with array_flip()
- Missing parentheses in function call
$arr = ['a' => 'apple', 'b' => 'banana', 'c' => 'apple']; $result = array_flip(array_unique($arr)); print_r($result);
Solution
Step 1: Apply
This removes duplicate values, so 'apple' at key 'c' is removed. Result: ['a' => 'apple', 'b' => 'banana']array_unique()on$arrStep 2: Apply
Keys and values swap: 'apple' becomes key with value 'a', 'banana' becomes key with value 'b'.array_flip()on the unique arrayFinal Answer:
Array ( [apple] => a [banana] => b ) -> Option AQuick Check:
Unique then flip = keys and values swapped without duplicates [OK]
- Flipping before removing duplicates
- Expecting duplicate keys in flipped array
- Confusing keys and values in output
$arr = ['x' => 'dog', 'y' => 'cat', 'z' => 'dog']; $result = array_flip($arr); print_r($result);What is the problem with this code?
Solution
Step 1: Understand
When flipping, values become keys. Duplicate values cause keys to overwrite previous ones.array_flip()behavior with duplicatesStep 2: Analyze the example
'dog' appears twice as a value. After flipping, only the last key ('z') remains for 'dog'.Final Answer:
The duplicate values cause keys to be overwritten in the flipped array -> Option DQuick Check:
Duplicate values overwrite keys when flipped [OK]
- Expecting all keys to be preserved
- Thinking array_flip causes syntax errors
- Assuming string keys cannot be flipped
$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?
Solution
Step 1: Remove duplicate usernames with
This keeps the first occurrence of each username, removing duplicates.array_unique()Step 2: Flip keys and values with
Swaps usernames to keys and user IDs to values, as required.array_flip()Final Answer:
$uniqueUsers = array_flip(array_unique($users)); -> Option CQuick Check:
Unique usernames first, then flip keys and values [OK]
- Flipping before removing duplicates
- Applying unique after flipping
- Assuming array_unique works on keys
Ready to prove it?
Pass the test and this pattern moves unknown
