array flip example
📝 PHPSwap 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
nice tip, just keep in mind that arrayflip will overwrite duplicates silently, so only use it when values are unique.