← Back to Gists

array flip example

📝 PHP
mcdonaldjamie520
mcdonaldjamie520 · Level 10 ·

Swap keys and values in any array with arrayflip to instantly enable reverse lookups.

PHP
$array = ['apple' => 'fruit', 'carrot' => 'vegetable', 'chicken' => 'meat'];
$flipped = arrayflip($array);
echo 'Original key for "vegetable": ' . $flipped['vegetable']; // Outputs: carrot

Comments

1
margaret19103 margaret19103

nice tip, just keep in mind that arrayflip will overwrite duplicates silently, so only use it when values are unique.