array_flip()
array_flip() — Exchanges all keys with their associated values in an array.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
array_flip() returns an array in flip order, i.e. keys from array become values and values from array become keys.
Signature
array_flip(array $array): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $array | array | An array of key/value pairs to be flipped. |
Return value
Returns the flipped array.
Example
A runnable snippet:
$input = array("oranges", "apples", "pears"); $flipped = array_flip($input); print_r($flipped);
Output:
Array ( [oranges] => 0 [apples] => 1 [pears] => 2 )
See also
array_values(), array_keys(), array_reverse()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_flip() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.