Current Location: Home> Latest Articles> Use array_flip() to implement fast mapping of permission identifiers to permission names

Use array_flip() to implement fast mapping of permission identifiers to permission names

M66 2025-06-03

When developing permission management systems, we often need to convert between permission data in different formats. For example, the permission identifiers are stored in the database (such as view_user , edit_post ), but more friendly permission names are required when displaying the interface (such as "View User" and "Edit Article").

If you maintain a permission mapping array, the array_flip() function in PHP can help you quickly implement the reverse mapping of identifiers to permission names. This article will talk about its usage and precautions.

1. Basic structure of permission mapping

Typically, we define an array of permission names to permission identifiers, for example:

 $permissions = [
    'View users' => 'view_user',
    'Edit User' => 'edit_user',
    'Delete users' => 'delete_user',
    'Post an article' => 'publish_post',
    'Edit the article' => 'edit_post',
];

This structure is suitable for rendering drop-down options or check boxes on the UI, but if the system gets an identifier (for example, the API returns edit_post ), how can we quickly find the corresponding Chinese name?

2. Use array_flip() to implement reverse mapping

PHP's array_flip() can combine keys and values ​​of an array, so it's very easy to create a mapping table from permission identifier to permission name:

 $codeToName = array_flip($permissions);

At this time, the structure of $codeToName becomes:

 [
    'view_user' => 'View users',
    'edit_user' => 'Edit User',
    'delete_user' => 'Delete users',
    'publish_post' => 'Post an article',
    'edit_post' => 'Edit the article',
]

Next, you can quickly obtain the corresponding name through the permission identifier:

 $code = 'edit_post';
echo $codeToName[$code]; // Output:Edit the article

3. Combined with practical application scenarios

Suppose you have a permission checking interface that returns an array of permission identifiers:

 $userPermissions = ['view_user', 'edit_post'];

You want to display the Chinese names of these permissions in the front-end interface, you can do this:

 $permissionNames = array_map(function($code) use ($codeToName) {
    return $codeToName[$code] ?? 'Unknown permissions';
}, $userPermissions);

print_r($permissionNames);

The output result is:

 Array
(
    [0] => View users
    [1] => Edit the article
)

4. Things to note

  1. Values ​​cannot be repeated : array_flip() requires that the value of the original array cannot be repeated, otherwise the following will overwrite the previous one. For example:

     $arr = ['A' => 'x', 'B' => 'x'];
    var_dump(array_flip($arr)); // Only retain 'x' => 'B'
    
  2. Suitable for static mapping : array_flip() is usually suitable for systems with relatively fixed permission structure. If permissions are generated dynamically at runtime, it is recommended to use a database or cache scheme to handle reverse mapping.

  3. Combined with cache to improve performance : If permission data comes from a database or configuration file, the mapping relationship can be cached to reduce the overhead of each page request.

5. Summary

Using array_flip() is a simple, fast and efficient method suitable for mutual conversion of "Permission Identifier? Permission Name" in permission systems. Especially when dealing with interface display or logging, development efficiency and code readability can be greatly improved.

If you are developing a PHP-based permission system, try applying array_flip() to your mapping logic and enjoy the convenience it brings!