PHP array_flip() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_flip() swaps the keys and the values of an array. The old values become the new keys, and the old keys become the new values.

You use array_flip() to turn a list into a quick lookup table, because checking isset($flipped['x']) is much faster than searching with in_array() on large arrays. It is also useful for reversing a map such as a code to name table.

Syntax

Call array_flip() with the values you need. Start with this line.

PHP
$flipped = array_flip($fruits);
CallWhat it does
array_flip(['apple', 'banana'])Returns ['apple' => 0, 'banana' => 1].
array_flip(['a' => 'x', 'b' => 'y'])Returns ['x' => 'a', 'y' => 'b'].
array_flip(['a' => 1, 'b' => 1])Returns [1 => 'b'], because the duplicate value keeps the later key.

Only integer and string values can become keys. Other values, such as floats or arrays, cause a warning and are skipped. The original array is not changed.

Library declarations
PHP
array_flip(array $array): array

Parameters

ParameterDescription
arrayRequired. The array to flip. Its values must be integers or strings so they can be used as keys.

Return Value

Return ValueDescription
arrayThe flipped array, with the old values as keys and the old keys as values.

PHP Examples

Example 1 — Swapping Keys and Values

Flip a list so that each value becomes a key that points to its old position.

PHP
<?php
$fruits = ['apple', 'banana', 'cherry'];

print_r(array_flip($fruits));

How It Works

  1. The list has the keys 0, 1 and 2 and the values 'apple', 'banana' and 'cherry'.
  2. array_flip() uses each value as a new key.
  3. Each old key becomes the new value.
  4. The result tells you the position of every fruit.

Example 2 — Duplicate Values

Two items with the same value cannot both survive, because keys must be unique.

PHP
<?php
$data = ['a' => 1, 'b' => 1, 'c' => 2];

print_r(array_flip($data));

How It Works

  1. Both 'a' and 'b' have the value 1.
  2. After flipping, 1 would be the key for both of them.
  3. The later one, 'b', overwrites the earlier one, 'a'.
  4. The result has only two items, and the key 'a' is lost.

Example 3 — A Fast Lookup Table

Flip a list of allowed words, then use isset() to test membership.

PHP
<?php
$allowed = array_flip(['admin', 'editor']);

var_dump(isset($allowed['editor']));
var_dump(isset($allowed['guest']));

How It Works

  1. array_flip() turns the list into the keys 'admin' and 'editor'.
  2. isset($allowed['editor']) checks whether that key exists.
  3. The key exists, so the first line shows true, even though its value is 1.
  4. The key 'guest' does not exist, so the second line shows false.

Frequently Asked Questions

It swaps the keys and the values of an array, so the old values become the new keys and the old keys become the new values.
Only the last key for each value is kept, because keys must be unique. The earlier entries are lost.
Only integers and strings. Other types such as floats, booleans, null and arrays cannot be keys, so PHP shows a warning and skips them.
After flipping a list, you can check for a value with isset($flipped[$value]), which is much faster than in_array() on big arrays.
No. It returns a new array and leaves the original array as it is.

Next: array_intersect()

array_intersect() returns values from the first array that also appear in every other arra.

array_intersect() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful