-
Notifications
You must be signed in to change notification settings - Fork 0
flipArray
Corey Avis edited this page Sep 23, 2026
·
2 revisions
Flips an array, swapping keys and values, but uses conditional logic to handle two special cases:
- Duplicate Values: If multiple keys share the same value, the resulting key will have an array containing all the original keys.
- Nested Numerical Arrays: If a value is a numerically indexed array, the nested values are extracted and become new keys in the output, using the original parent key as their value. Otherwise, all other entries flip as normal.
There are examples below with a comparison to PHP's
array_flipfunction.
flipArray(array $array): array
array (array) : Array to flip.
(array) : Returns the flipped array.
$fx->flipArray(['a' => 10, 1 => 'apple'])
// Result: [10 => 'a', 'apple' => 1]
| Array [^1] | flipArray [^2] | array_flip [^3] |
|---|---|---|
['a' => 10, 1 => 'apple'] |
[10 => 'a', 'apple' => 1] |
[10 => 'a', 'apple' => 1] |
['one', 'two', 'three'] |
['one' => 0, 'two' => 1, 'three' => 2] |
['one' => 0, 'two' => 1, 'three' => 2] |
['opt' => ['a' => 'one', 'b' => 'two']] |
['opt' => ['one' => 'a', 'two' => 'b']] |
[null] |
['int' => ['one', 'two', 'three']] |
['one' => 'int', 'two' => 'int', 'three' => 'int'] |
[null] |
['one' => 'int', 'two' => 'int', 'three' => 'int'] |
['int' => ['one', 'two', 'three']] |
['int' => 'three'] |
['num', 'num', 'num'] |
['num' => [0, 1, 2]] |
['num' => 2] |
[^1]: The array supplied to each function.
[^2]: The output from the supplied array using the flipArray method.
[^3]: The output from the supplied array using the array_flip function.
- Triggers a warning if the argument is an empty array.