What is PHP array_flip?
Description
PHP array_flip exchanges array keys with their values.
Function Signature
array_flip(array $array): array
Argument
- $array – Input array
Return Type
This function returns the flipped array.
Notes
- The values must be either int or string types; otherwise, the function raises a warning. The function excludes such a key/value pair from the flipped array.
- A recurring value will get the latest key, and the other pairs will be discarded.
Introduction – PHP array_flip
PHP array_flip swaps the keys and values of an array. The keys in the associative arrays are always unique, while values can repeat. Secondly, associative array keys are either string or int types. So, the array_flip discards a value and its key if the value type is other than string or int.

As the keys are supposed to be unique, the function assigns the latest key as a value to the recurring values in the input array. Don’t worry if these don’t make sense because this article includes some excellent examples that reveal every aspect of the PHP array_flip.
Usage Examples – PHP array_flip
#1 Basic Example – PHP array_flip
Here’s a straightforward example of the array_flip function.
<?php
$currencies =
[
"United States Dollar" => "USD",
"Australian Dollar" => "AUD",
"Canadian Dollar" => "CAD",
"British Pound" => "GBP",
"Chinese Yuan" => "CNY",
"Singapore Dollar" => "SGD"
];
print_r(array_flip($currencies));
/*
OUTPUT
Array
(
[USD] => United States Dollar
[AUD] => Australian Dollar
[CAD] => Canadian Dollar
[GBP] => British Pound
[CNY] => Chinese Yuan
[SGD] => Singapore Dollar
)
*/
?>
Voila! It swaps the keys and values without any problem.
#2 – Recurring Values Example
Let’s extend the currency array and add some other entries before flipping it.
<?php
$currencies =
[
"United States Dollar" => "USD",
"Australian Dollar" => "AUD",
"Canadian Dollar" => "CAD",
"British Pound" => "GBP",
"Chinese Yuan" => "CNY",
"Singapore Dollar" => "SGD",
"Germany" => "EUR",
"France" => "EUR",
"Ireland" => "EUR"
];
print_r(array_flip($currencies));
/*
OUTPUT
Array
(
[USD] => United States Dollar
[AUD] => Australian Dollar
[CAD] => Canadian Dollar
[GBP] => British Pound
[CNY] => Chinese Yuan
[SGD] => Singapore Dollar
[EUR] => Ireland
)
*/
?>
Just as expected, in this particular example, “EUR” was a duplicate value. Remember that keys are unique. So when the function flips, it has to keep only one “EUR” key. The possible values are “Germany,” “France,” and “Ireland.” Observe that “Ireland” is the latest one. So, the function assigns “Ireland” as a value to “EUR.”
#3 – Invalid Value Type
The article mentions that a value must be either of the type int or string to become a key. Let’s test it out on invalid value types and see the output.
<?php
$nums =
[
"Zero" => 0,
"One" => 1.0,
"Two" => 2.0,
"Three" => 3.0,
"Four" => 4,
"Five" => "5"
];
print_r(array_flip($nums));
/*
OUTPUT
Array
(
[0] => Zero
[4] => Four
[5] => Five
)
*/
?>
So, the array_flip includes the valid value types only in the flipped array. It discards all the invalids and also raises a warning.
PHP Warning: array_flip(): Can only flip string and integer values
PHP array_flip vs in_array
PHP in_array function checks if a value exists in an array. This function is quite helpful but could be slower for really chunky arrays. The array_flip works much faster, and there is a way to check if a value exists in an array.
Here’s how the in_array works.
<?php
$currencies =
[
"USD" => "United States Dollar",
"AUD" => "Australian Dollar",
"CAD" => "Canadian Dollar",
"GBP" => "British Pound",
"CNY" => "Chinese Yuan",
"SGD" => "Singapore Dollar",
"EUR" => "Ireland",
];
if(in_array("United States Dollar", $currencies)) {
echo "PHP in_array: United States Dollar exists in the currencies array\n";
}
else{
echo "PHP in_array: United States Dollar doesnot exist in the currencies array\n";
}
if(in_array("New Zealand Dollar", $currencies)) {
echo "PHP in_array: New Zealand Dollar exists in the currencies array\n";
}
else{
echo "PHP in_array: New Zealand Dollar doesnot exist in the currencies array\n";
}
/*
OUTPUT
PHP in_array: United States Dollar exists in the currencies array
PHP in_array: New Zealand Dollar doesnot exist in the currencies array
*/
?>
Here’s how to check with array_flip.
<?php
$currencies =
[
"USD" => "United States Dollar",
"AUD" => "Australian Dollar",
"CAD" => "Canadian Dollar",
"GBP" => "British Pound",
"CNY" => "Chinese Yuan",
"SGD" => "Singapore Dollar",
"EUR" => "Ireland",
];
$flipped_curr = array_flip($currencies);
if(isset($flipped_curr["United States Dollar"])) {
echo "PHP in_array: United States Dollar exists in the currencies array\n";
}
else{
echo "PHP in_array: United States Dollar doesnot exist in the currencies array\n";
}
if(isset($flipped_curr["New Zealand Dollar"])) {
echo "PHP in_array: New Zealand Dollar exists in the currencies array\n";
}
else{
echo "PHP in_array: New Zealand Dollar doesnot exist in the currencies array\n";
}
/*
OUTPUT
PHP in_array: United States Dollar exists in the currencies array
PHP in_array: New Zealand Dollar doesnot exist in the currencies array
*/
?>
Perfect! The isset function returns false if a key does not exist. So, the array_flip swaps the keys and values, and that’s why the isset function works for the flipped array.
Conclusion
This article explains PHP array_flip with several examples and reveals the associated caveats. Besides, it also describes how to search for a value in an array using the array_flip. That’s pretty much all about this function. If you want to learn more about PHP, don’t miss out on awesome PHP content at FuelingPHP.
Want to learn more about PHP?
We have many fun articles related to PHP. You can explore these to learn more about PHP.