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 Multidimensional Arrays: Data Manipulation

In web development, managing and manipulating data is one of the most common tasks. When it comes to storing complex…

MongoDB CRUD with PHP: Create, Read, Update, and Delete

PHP gives us the ability to work with MongoDB, highlighting the importance of CRUD (Create, Read, Update, Delete) operations for…

PHP array_rand() Function: Usage & Examples

The array_rand() is a helper function in PHP that makes it easier when dealing with arrays; you can just pull…

PHP filter_input: How to Validate Input Securely

User input can be risky. Hackers exploit weak validation to inject malicious data. PHP filter_input() helps you sanitize and validate…

PHP Integers Guide: Types, Limits, and Conversions

You use integers (int) in PHP to count items, set page numbers, handle IDs, and manage loop counters. In this…

PHP Singleton Pattern: How to Use with Examples

The PHP singleton design pattern makes sure that the class has only one instance and provides a global access point…

PHP foreach Loop: How to Access Keys & Values in Arrays

PHP foreach loop came to help work with array data in a simple way. Arrays and objects grew more common…

How to Select Data Using PHP and MySQL?

In this article, You will understand everything you need to know about how to use PHP to select data from…

PHP mb_substr Function: How it Works with Examples

You may need to get part of a text when you work with text in PHP. The mb_substr gives you…

PHP continue Statement: How to Skip Steps in Iteration

The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…

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.