In daily PHP development, we often encounter mutual conversion between "enumeration values? tags". For example, an array of order status:
$statusMap = [
0 => 'To be paid',
1 => 'Paid',
2 => 'Shipped',
3 => 'Completed',
];
This structure facilitates the extraction of corresponding text descriptions from the status code. But sometimes we also need to operate the other way around, such as taking out its corresponding status code 2 based on "shipped". At this time, if there is no additional structural support, you may need to traverse the entire array and write similar logic:
function getStatusCode($label) {
foreach ($statusMap as $code => $desc) {
if ($desc === $label) {
return $code;
}
}
return null;
}
It looks a bit long-winded. At this time, the array_flip() function that comes with PHP comes in handy!
array_flip() is a built-in function in PHP that is used to pair the "key" and values of an array:
array_flip(array $array): array
Notes:
The value of the array must be unique and scalar values that can be used as keys (such as strings or integers).
If there are duplicate values, the following will overwrite the previous one.
Let's take a look at how to gracefully reverse an array of enum values:
$statusMap = [
0 => 'To be paid',
1 => 'Paid',
2 => 'Shipped',
3 => 'Completed',
];
$labelToCodeMap = array_flip($statusMap);
echo $labelToCodeMap['Shipped']; // Output 2
Is it a lot simpler in an instant? It only takes array_flip() once to get a mapping table from "label" to "value".
Suppose you have an enumeration of user gender options:
$genderMap = [
'M' => 'male',
'F' => 'female',
'O' => 'other',
];
When submitting the form, the user selects 'female' , which you want to convert back to 'F' and store in the database:
$input = 'female';
$labelToValue = array_flip($genderMap);
$genderCode = $labelToValue[$input] ?? null;
if ($genderCode === null) {
echo "Illegal gender value";
} else {
// Suppose it is saved to the database
saveGenderToDB($genderCode);
}
Not only is the code more compact, but the logic is also very clear and easy to understand.
If you need to use inverted arrays in multiple places, you might as well define a common method in your project:
function getFlippedMap(array $map): array {
static $cache = [];
$hash = md5(json_encode($map));
if (!isset($cache[$hash])) {
$cache[$hash] = array_flip($map);
}
return $cache[$hash];
}
In this way, even if you call it multiple times, the existing inversion results can be reused to improve performance.
Remember that if the values in the original array are not unique, array_flip() will automatically overwrite the previous key:
$arr = [
'a' => 'apple',
'b' => 'banana',
'c' => 'apple',
];
print_r(array_flip($arr));
The output will be:
Array
(
[banana] => b
[apple] => c // 'a' Covered
)
This can cause bugs when dealing with enum values, so it is recommended to make sure the values are unique before inversion.
array_flip() is a small but very practical function, especially suitable for fast conversions between "label? values". Through it, we can make PHP code more concise, easier to read, and less errors. Next time you write a loop, you might as well think about it: Can you use array_flip() to get it in one step?