Example 1 — Swapping Keys and Values
Flip a list so that each value becomes a key that points to its old position.
<?php
$fruits = ['apple', 'banana', 'cherry'];
print_r(array_flip($fruits)); How It Works
- The list has the keys 0, 1 and 2 and the values 'apple', 'banana' and 'cherry'.
- array_flip() uses each value as a new key.
- Each old key becomes the new value.
- The result tells you the position of every fruit.

