PHP array_flip Function: How it Works with Examples

php array flip

PHP array_flip swaps keys with values. It works well when you need values as keys in fast lookups.

What is the array_flip Function in PHP?

The array_flip function swaps all keys with values in an array. The result holds values as keys and keys as values.

The syntax looks like this:

array_flip(array $array)
  • It takes one parameter: an array with key and value pairs.
  • It returns a new array with swapped keys and values.

You can use array_flip to switch keys and values. It works best when you need values as keys for fast checks or mappings.

Here is a quick example:

$data = ["a" => 1, "b" => 2];
$result = array_flip($data);
print_r($result);

Here is the output:

Array
(
[1] => a
[2] => b
)

It swaps the values with keys and returns the new array.

The function loops through each element. It sets the value as the new key. It then sets the key as the new value.

If two or more values match, the last one replaces the others. The function cannot hold duplicate keys. Only the last pair stays in the final array.

Examples of array_flip in PHP

Here is an example:

$colors = ["red" => 1, "blue" => 2, "green" => 3];
$result = array_flip($colors);
print_r($result);

This shows the swap of color names with numbers. Each value turns into the new key and each key turns into the new value. Here is the output:

Array
(
[1] => red
[2] => blue
[3] => green
)

Here is another example:

$letters = ["x" => 10, "y" => 20, "z" => 20];
$result = array_flip($letters);
print_r($result);

This shows the effect of duplicate values. The key for “y” is lost because “z” also holds 20. The last one replaces the earlier one.

The output:

Array
(
[10] => x
[20] => z
)

You can also flip an array with mixed keys. Here is an example:

$data = [100 => "cat", 200 => "dog", 300 => "bird"];
$result = array_flip($data);
print_r($result);

This swaps numbers with animal names. Each number becomes a new value and each name becomes a new key.

The output:

Array
(
    [cat] => 100
    [dog] => 200
    [bird] => 300
)

Wrapping Up

In this article you learned what array_flip does and how it swaps keys with values. You also saw what happens with duplicates and how mixed keys behave.

Here is a quick recap:

  • array_flip swaps all keys and values.
  • It returns a new array after the swap.
  • Duplicate values overwrite earlier keys.
  • You can flip arrays with numbers, text, or boolean values.

FAQs

What does PHP array_flip function do?

The array_flip() function in PHP exchanges keys and values in an array. For example:
$data = ["a" => 1, "b" => 2, "c" => 3];
$result = array_flip($data);
print_r($result);
Output:
Array
(
    [1] => a
    [2] => b
    [3] => c
)

How does PHP array_flip handle duplicate values?

When array_flip() finds duplicate values, only the last one is kept as a key. For example:
$data = ["x" => 1, "y" => 1, "z" => 2];
$result = array_flip($data);
print_r($result);
Output:
Array
(
    [1] => y
    [2] => z
)

Can array_flip work with multidimensional arrays?

No, array_flip() only works on single level arrays. If a value is an array, it throws an error. For example:
$data = ["x" => [1,2], "y" => 3];
$result = array_flip($data);
This produces: Warning: array_flip(): Can only flip STRING and INTEGER values

What are real use cases of PHP array_flip?

Some real use cases include:
  • Removing duplicates from an array
  • Quick lookup of values as keys
  • Swapping key-value pairs for mapping
Example:
$items = ["apple", "banana", "apple", "orange"];
$result = array_keys(array_flip($items));
print_r($result);
Output:
Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)

Similar Reads

PHP array_merge_recursive: Merge Arrays Deeply

PHP array_merge_recursive joins two or more arrays into one nested array and keeps all values with their keys. Syntax of…

PHP array_multisort Function with Examples

PHP array_multisort sorts many arrays or a multi level array in one go. It can sort data in numeric order…

PHP compact Function: Assoc Array from Variables

The PHP compact function is a way to pull variables into an array. In this article, you will learn how…

PHP Arrow Functions: Understanding “fn” Syntax

Arrow functions were introduced in PHP 7.4. They offer you a way to write simple operations, such as calculations, filters,…

How to Use PHP count() to Get Array Length

PHP developers may be need to determine the number of elements in an array. The count() function in PHP makes…

PHP Namespace: How to Group Code (With Example)

PHP namespace solves the problem of name conflicts. Different developers may create functions-classes, or constants with the same name. PHP…

PHP explode Function: How it Works with Examples

When you deal with text in PHP, sometimes you need to break it into parts. This is common when users…

PHP array_keys: How to Extract Keys in Arrays with Examples

This guide shows how PHP array_keys finds all keys in an array and how you can filter them by a…

PHP fwrite: How to Write Data to Files in PHP

actions, store settings, or even create log files for debugging. That’s where PHP fwrite comes in. It writes data to…

PHP Comment: How to Write Comments in PHP

let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…

Previous Article

JavaScript toReversed Function with Examples

Next Article

Node.js Streams Guide: How to Handle Data with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.