PHP array_flip() Function

Example

Reverse all keys and their associated values in the array:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result);
?>

Run Instance

Definition and Usage

The array_flip() function is used to reverse/swap all keys and their associated values in the array.

The array_flip() function returns a reversed array. If the same value appears multiple times, the last key name will be used as its value, and all other key names will be lost.

If the data type of the value in the original array is not a string or integer, the function will report an error.

Syntax

array_flip(array);
Parameter Description
array Required. Specifies the array to be reversed for key/value pairs.

Technical Details

Return Value: If the reversal is successful, the reversed array is returned. If it fails, NULL is returned.
PHP Version: 4+