PHP 8 reference · Array Functions
PHP array_flip() Function
Swap keys and values, so each value becomes a key. Values must be strings or integers; duplicates keep the last key.
PHP 8 Since 4.0
Syntax
array_flip(array $array): array
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
Return value
The flipped array.
Examples
<?php
print_r(array_flip(["a" => 1, "b" => 2]));
Output
Array
(
[1] => a
[2] => b
)<?php
$allowed = array_flip(["jpg", "png", "gif"]);
var_dump(isset($allowed["png"]), isset($allowed["exe"]));
Output
bool(true) bool(false)
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
Related
array_keys()· Return all the keys of an array, or only the keys whose value matches a search value.array_combine()· Create an array by using one array for keys and another for values. Both arrays must have the same number of elements.in_array()· Check whether a value exists in an array. Pass true as the third argument for a strict (===) comparison.- PHP Arrays · tutorial
- PHP Array Functions · tutorial