Understanding the PHP array_flip Function
The PHP function array_flip is a simple and useful tool for flipping the keys and values of an array. This means that the keys in the original array become the
The PHP function array_flip exchanges the keys and values of an array: every key becomes a value, and every value becomes a key. It returns a brand-new array and never modifies the one you pass in. This page covers the syntax, a runnable example, how duplicates and key types are handled, common use cases, and the related functions worth knowing.
Syntax
array_flip(array $array): array$array— the input array. Its values must be valid keys, meaning they can only be integers or strings. Any other value type (booleans, floats, arrays, objects,null) triggers a warning and is skipped in the result.- Return value — a new array with the flipped key/value pairs.
How to Use the PHP array_flip Function
Pass your array to the function and it returns the flipped array:
PHP Example of array_flip function usage
In this example, $flipped_array will contain the following:
Array
(
[1] => a
[2] => b
[3] => c
)The string keys "a", "b", "c" are now the values, and the integers 1, 2, 3 are now the keys.
Benefits of Using the PHP array_flip Function
There are several benefits to using array_flip in your PHP code. These include:
- Simplifying your code:
array_flipcan help simplify your code by eliminating the need for manually swapping keys and values in an array. - Improving readability: Flipped arrays can often be easier to read and understand, especially if you are working with large arrays or arrays that contain complex data structures.
- Optimized execution:
array_flipis a built-in function optimized for this task, typically outperforming manual key-value swapping loops.
Building a Fast Lookup Table
The most idiomatic use of array_flip is turning a list of values into a set you can probe with isset(). Because keys are hashed, an isset() check on a flipped array is O(1) — far faster than scanning the list with in_array() on a large dataset.
<?php
$allowed = ["admin", "editor", "viewer"];
$lookup = array_flip($allowed);
var_dump(isset($lookup["editor"])); // present
var_dump(isset($lookup["guest"])); // missing
?>Output:
bool(true)
bool(false)Handling Duplicate Values
When two keys in the original array share the same value, the flipped result can only have one entry for that value — so the last key wins and the earlier one is dropped:
<?php
$colors = ["red", "green", "blue", "green"];
print_r(array_flip($colors));
?>Output:
Array
(
[red] => 0
[green] => 3
[blue] => 2
)Index 1 (the first "green") is lost because index 3 overwrote it. If you instead need to count how many times each value occurs, use array_count_values(); to remove duplicates while keeping order, use array_unique().
Common Use Cases
- Building lookup sets from a list of unique IDs or allowed values for fast
isset()checks. - Inverting a dictionary or mapping (e.g.
name => idintoid => name). - Reordering an array's elements as keys while remembering their original position.
Limitations and Gotchas
- Duplicate values: only the last key for a repeated value survives; earlier keys are lost (see above).
- Invalid value types: values that are not
intorstringcannot become keys. They emitWarning: Can only flip string and integer valuesand are skipped, while valid pairs are still flipped. - Numeric-string keys: when a value becomes a key, PHP applies normal array-key rules — a numeric string like
"42"becomes the integer42.
Related Functions
array_reverse()— reverses element order (often confused with flipping keys/values).array_keys()andarray_values()— extract just the keys or just the values.array_search()— find the key for a single value without flipping the whole array.array_combine()— build an array by pairing one array of keys with one array of values.
To go deeper on arrays in general, see PHP Arrays and Associative Arrays.
Conclusion
array_flip swaps the keys and values of an array in a single, optimized call. It shines when you need a fast isset() lookup set or want to invert a mapping — just remember that values must be integers or strings, and that duplicate values collapse to their last key.