array_flip

Swaps every key with its value, turning an array's values into keys and its keys into values.

Signature
array_flip(array $array): array
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$arrayarrayAn array of key/value pairs to flip. Values must be int or string to become keys.

Return value

arrayThe flipped array, or null on failure in very old versions; in PHP 8 it always returns an array.

Errors

A value that is not an int or string emits a warning and that pair is skipped; the function does not throw.

array_flip() exchanges keys and values. Its real use is not novelty — it is building a lookup table. Checking membership with in_array() is a linear scan; flipping a list of allowed values once and then using isset() or array_key_exists() turns every later check into a hash lookup.

What can become a key

Only int and string values survive. Floats, booleans, nulls, arrays and objects trigger a warning and their pair is dropped from the result, so a flipped array can be shorter than the original without you noticing. Numeric strings are also coerced: the value "7" becomes the integer key 7.

Duplicates collapse

Keys are unique, so if two entries share a value, only the last one wins. ['a' => 'x', 'c' => 'x'] flips to ['x' => 'c']. This is silent, and it is the single most common source of surprise. If you need every original key, use array_keys($arr, $value) instead, which returns all of them.

Pairing it with array_intersect_key

A very common idiom is array_intersect_key($data, array_flip($allowedFields)) — a one-line whitelist filter that keeps only the named keys.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Turn a list of allowed field names into a key-lookup set for fast isset() checks.
  • Whitelist an input array with array_intersect_key($input, array_flip($allowed)).
  • Invert an id-to-slug map so you can look the id up by slug.

Frequently asked questions

What happens to duplicate values when you use array_flip?

Only the last key with that value is kept, because keys must be unique. The earlier keys are lost with no warning.

Why does array_flip emit a warning on my array?

Because at least one value is not an int or a string — a float, bool, null, array or object cannot be an array key. That pair is skipped and the rest of the array is still flipped.

Is array_flip faster than in_array for checking membership?

Yes, if you check more than a couple of times. Flipping costs one pass, after which isset() lookups are constant time, whereas every in_array() call scans the whole array.

More examples

Fast membership testing

After flipping once, each check is an O(1) hash lookup instead of the O(n) scan in_array() would do. The output shows ok.example allowed and bad.example blocked.

Example · php
<?php
$blocked = ['spam.example', 'bad.example', 'evil.example'];
$lookup = array_flip($blocked);

foreach (['ok.example', 'bad.example'] as $host) {
    $hit = isset($lookup[$host]) ? 'blocked' : 'allowed';
    echo $host, ': ', $hit, "\n";
}

Whitelisting keys

array_flip turns the allowed list into keys so array_intersect_key can match against it. The dangerous is_superuser field is dropped and array_key_exists confirms it is gone.

Example · php
<?php
$input = ['name' => 'Ada', 'role' => 'admin', 'is_superuser' => true];
$allowed = ['name', 'role'];

$safe = array_intersect_key($input, array_flip($allowed));
print_r($safe);
var_dump(array_key_exists('is_superuser', $safe));

Discussion

  • Be the first to comment on this lesson.