Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_flip()

Exchange keys and values to build a reverse lookup.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

array_flip(array $array): array

Exchange keys and values to build a reverse lookup. Values must be integers or strings to serve as keys. If several entries contain the same value, the last original key survives in the reverse map. Reversing twice therefore need not reconstruct the initial data.

Signature

array_flip(array $array): array

Example output

{"A":"new","H":"hold"}

Examples

<?php
$codes = ['old'=>'A', 'new'=>'A', 'hold'=>'H'];
echo json_encode(array_flip($codes));

Result

{"A":"new","H":"hold"}

Sources