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 Switch | How the Switch Statement Works in PHP

The PHP switch statement is multiple conditions to only execute one block if its condition generating a true boolean value.…

PHP array_filter: How to Filter Array Values with Examples

You can use array_filter in PHP to remove unwanted data from arrays. It works with a custom callback or default…

PHP is_file Function: Check if a File Exists

If you are working with PHP and need a way to confirm if something is indeed a file, the is_file function will…

History of PHP: From PHP/FI to Modern Web Development

You use PHP every day if you build websites, but most people do not know where it came from or…

PHP array_intersect Function: How it Works with Examples

The PHP array_intersect function finds common values in arrays and returns matches as a new array. Understand the array_intersect in…

PHP AND (&&) Operator: Usage & Examples

As you are entering into PHP, it would be essential to understand how logical operators work. The PHP AND operator…

PHP $_FILES: How to Upload Files in PHP

The PHP superglobal $_FILES is a very important utility and great for dealing with file uploads on the web. When a user…

PHP str_repeat: How it Works with Examples

PHP gives you many string tools. One of them is php str_repeat. This function repeats a texts as many times…

PHP Class Destructor: How It Works with Examples

A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…

PHP array_intersect_assoc: How it Works with Examples

PHP array_intersect_assoc compares values and keys in arrays. It returns matches with the same key and value from all arrays…

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.