Home >>PHP Array Functions >PHP array_flip() Function
PHP array_flip() Function is used to exchange elements within an array. It exchanges all the keys with their associated values in an array and all the values with their associated keys. It accepts only one parameter $array that is the input array. It returns another array with all the elements exchanged or flipped and null if the input array is invalid.
Syntax:
array_flip($array);
Parameter | Description |
---|---|
array | This is a required parameter. This parameter defines an array of key and value pairs to be flipped. |
Here is an example of array_flip() function in PHP:
<html> <body> <pre> <?php $x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5"); $result=array_flip($x); print_r($result); ?> </pre> </body> </html>
Array ( [1] => a [2] => b [3] => c [4] => d [5] => e )
Example 2:
<html> <body> <pre> <?php $x=array("Abhi"=>"PHP","Rohit"=>"Java","Rahul"=>"HTML","Mohit"=>"CSS","Mickey"=>"PHP","Jerry"=>"Python"); $result=array_flip($x); print_r($result); ?> </pre> </body> </html>
Array ( [PHP] => Mickey [Java] => Rohit [HTML] => Rahul [CSS] => Mohit [Python] => Jerry )