Signature
array_flip(array $array): arrayPurpose
array_flip() exchanges the keys and values of an array. Every original value becomes a key in the result, and the original key becomes the value stored under it. The most common use is turning a list of allowed strings into a lookup table, so you can test membership with isset() instead of scanning the list each time with in_array().
Signature
Parameters and defaults
$array is the only parameter and it has no default. It is the array whose keys and values you want to swap. The input array is not modified, because PHP passes it by value. The function returns a new array.
The values of the input must be usable as array keys, which means each one must be an int or a string. Other types cannot become keys.
Return value and failure cases
The function always returns an array. There is no false return and no exception for unusual contents. Two situations deserve attention:
- Invalid value types. If a value is not an
intor astring(for example a float, a boolean,nullor a nested array), PHP emits a warning and leaves that pair out of the result. - Duplicate values. If several elements share the same value, only one pair survives. The latest original key wins and the earlier keys are lost.
An empty array flips to an empty array.
Example 1: building a lookup table
Flipping a list gives you its values as keys, with each value's original position stored as the new value. Checking isset() on a key is a direct hash lookup.
$allowed = ['draft', 'published', 'archived'];
$lookup = array_flip($allowed);
echo json_encode($lookup, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
echo isset($lookup['published']) ? "valid" : "invalid", "\n";
echo isset($lookup['deleted']) ? "valid" : "invalid", "\n";{"draft":0,"published":1,"archived":2}
valid
invalid
The stored positions are 0, 1 and 2. Because they are never null, isset() works reliably here.
Example 2: duplicate values collide
When two entries share a value, the flipped array can hold only one key for it. The entry that appears later in the input overwrites the earlier one.
$input = ['a' => 1, 'b' => 1, 'c' => 2];
$flipped = array_flip($input);
echo json_encode($flipped, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"1":"b","2":"c"}
Key a is lost because b carries the same value 1 and comes later. The integer keys 1 and 2 do not start at zero, so JSON encodes the result as an object.
Example 3: values are converted to array keys
The new keys follow PHP's normal array key rules. A string that is a canonical decimal integer, such as "10", becomes the integer key 10. A string with a leading zero, such as "07", stays a string.
$flipped = array_flip(['10', 'abc', '07']);
var_dump(array_keys($flipped));array(3) {
[0]=>
int(10)
[1]=>
string(3) "abc"
[2]=>
string(2) "07"
}
This conversion happens silently, so the key type you read back may differ from the string you flipped.
Edge cases
- Nothing is recursive. Nested arrays are not flipped, and an array used as a value is an invalid type, so it triggers a warning and is skipped.
- Order follows first appearance. In the result, a repeated value keeps the position where it was first inserted, but holds the last original key. Flipping twice therefore removes duplicates, yet the surviving values may be paired with different keys than before, so it is not a faithful replacement for
array_unique(). - Original keys may be strings or integers. Either one becomes the new value with its type unchanged.
- The first flip is lossy. You cannot always recover the original array by flipping again, because collisions and skipped entries are gone for good.
Common mistakes
- Assuming values are unique. Flipping data such as status codes or categories often merges rows without any message. Check for duplicates first if losing entries would be a bug.
- Flipping floats, booleans or
null. These produce warnings and missing entries. Cast or filter the values first. - Expecting a reindexed list. The result is keyed by the old values, so it is not a sequential list unless those values happen to be
0, 1, 2, .... - Mixing up key types. A flipped
"10"becomes integer10. Strict comparisons against the string key can then fail. - Flipping for a single membership test. Building the flipped array costs a full pass. It pays off only when you check membership several times.
Related functions
array_values()returns the values of an array as a new list.array_keys()returns the keys, or only the keys matching a given value.array_reverse()reverses the element order without swapping keys and values.array_search()finds the key for a single value.array_unique()removes duplicate values directly.array_combine()builds an array from separate key and value lists.
Recap
array_flip() returns a new array with keys and values swapped. Values must be integers or strings, and anything else is skipped with a warning. Duplicate values collapse so the latest key wins, and string keys that look like integers become integer keys. It works best for building lookup tables from lists of unique strings.
Tested examples
$allowed = ['draft', 'published', 'archived'];
$lookup = array_flip($allowed);
echo json_encode($lookup, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
echo isset($lookup['published']) ? "valid" : "invalid", "\n";
echo isset($lookup['deleted']) ? "valid" : "invalid", "\n";{"draft":0,"published":1,"archived":2}
valid
invalid
$input = ['a' => 1, 'b' => 1, 'c' => 2];
$flipped = array_flip($input);
echo json_encode($flipped, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"1":"b","2":"c"}
$flipped = array_flip(['10', 'abc', '07']);
var_dump(array_keys($flipped));array(3) {
[0]=>
int(10)
[1]=>
string(3) "abc"
[2]=>
string(2) "07"
}