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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_flip(array $array): array

Parameters

ParameterDescription
array The input array.

Return value

The flipped array.

Examples

Swap keys and valuesTry it yourself
<?php
print_r(array_flip(["a" => 1, "b" => 2]));

Output

Array
(
    [1] => a
    [2] => b
)
Fast lookups with isset()Try it yourself
<?php
$allowed = array_flip(["jpg", "png", "gif"]);
var_dump(isset($allowed["png"]), isset($allowed["exe"]));

Output

bool(true)
bool(false)

Version history

VersionChange
PHP 4.0Added

Was this page helpful?