PHP array_flip() Function | PHP Array Functions

The array_flip() function exchanges the keys and values of an array. The values become the keys, and the keys become the values.

Key Features of array_flip()

Syntax of array_flip()

Syntax

array array_flip(array $array);

Example Using array_flip()

The following example demonstrates how array_flip() is used:

Example


$array = [
    "apple" => "red",
    "banana" => "yellow",
    "grape" => "purple"
];
$flippedArray = array_flip($array);
print_r($flippedArray);

Output

Array ( [red] => apple [yellow] => banana [purple] => grape )