The PHP array_flip Function is an inbuilt function in PHP which exchanges the keys with their values in the associative array. It returns an array with the corresponding keys and values exchanged. However, the values of the existing array should be valid keys ie. either string or integer. The function throws a warning if the new keys are not valid. In this article, we will discuss the array_flip Function. Also, we will discuss a few examples of using it.
Syntax
array array_flip(array);
Parameters
There is only one parameter in the array_flip Function. The parameter should be an array whose keys and values are to be exchanged.
Return Type
The PHP array_flip Function returns an array with the keys and values exchanged. However, it returns a NULL value if the input values are invalid.
Examples
Let us go through a couple of examples to demonstrate.
Example 1
<?php $testArray = array( 'John' => '10', 'Emma' => '20', 'Chandler' => '30' ); print_r(array_flip($testArray)); ?>
Output
Array
(
[10] => John
[20] => Emma
[30] => Chandler
)
In the example above, an array containing an integer as values and name as the key is passed in array_flip function. The keys and values are exchanged in the returned array.
Example 2
For instance, let’s consider an array containing an invalid value as a key.
<?php $testArray = array( 'John' => '10', 'Emma' => '20', 'Chandler' => array( 'email' => '30', 'age' => 12 ) ); print_r(array_flip($testArray)); ?>
Output
Array
(
[10] => John
[20] => Emma
)
In the above example, it can be observed that the key Chandler contains an array value which is an invalid key. The PHP array_flip function ignores that value in the output.
Conclusion
In this article, we discussed PHP array_flip Function. You may go through other PHP array functions on this page. Additionally, you may go through

Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.