Current Location: Home> Latest Articles> Combined with array_filter() to quickly implement permission filtering

Combined with array_filter() to quickly implement permission filtering

M66 2025-06-03

In web development, permission management is a very important part. Usually we need to restrict their access to certain resources based on the permissions of different users. In PHP, we can use the array_filter() function to efficiently filter permissions, especially when the user permission data is large, it can provide very good performance. This article will introduce how to quickly implement permission filtering function in combination with array_filter() .

Introduction to array_filter() function

PHP's array_filter() function is used to filter elements in an array. This function accepts an array and a callback function. The return value of the callback function is true or false . array_filter() will decide whether to retain elements in the array based on the return value of the callback function.

The basic syntax is as follows:

 array_filter(array $array, callable $callback, int $mode = 0): array
  • $array : The array to filter.

  • $callback : A callback function for filtering.

  • $mode : Optional parameter to determine the value passed to the callback function. 0 is the value transmission method, 1 is the key transmission method.

Basic example: Filter permissions using array_filter()

Suppose we have a permission list that records the resources that different users can access, and the array is as follows:

 $permissions = [
    'admin' => ['view_dashboard', 'edit_settings', 'view_reports'],
    'editor' => ['view_dashboard', 'view_reports'],
    'viewer' => ['view_dashboard'],
];

We want to filter resources that they have permission to access based on user roles. Assuming that the admin user needs to have permission to access all resources, the editor and viewer users are subject to some restrictions.

 $user_role = 'editor'; // Assume that it is currently an editor role

// Filter out resources with permissions in the current role
$user_permissions = $permissions[$user_role];

// List of resources that need to be filtered
$all_permissions = ['view_dashboard', 'edit_settings', 'view_reports', 'delete_post'];

$filtered_permissions = array_filter($all_permissions, function ($permission) use ($user_permissions) {
    return in_array($permission, $user_permissions);
});

print_r($filtered_permissions);

Output result:

 Array
(
    [0] => view_dashboard
    [2] => view_reports
)

Through this example, we can see that the array_filter() function helps us filter out the permissions of the current user role.

Combining array_filter() to implement complex permission filtering

In actual projects, permission filtering may be more complicated based on different rules, such as some permissions may have additional restrictions. For example, some operations can only be performed if certain conditions are met, or some resources require specific permission combinations.

Suppose we have the following permission data:

 $permissions = [
    'admin' => ['view_dashboard', 'edit_settings', 'view_reports'],
    'editor' => ['view_dashboard', 'view_reports'],
    'viewer' => ['view_dashboard'],
];

// User Roles
$user_role = 'admin';

// Additional user permissions restrictions
$user_restrictions = ['edit_settings']; // admin Roles do not allow editing settings

// Filter out resources with permissions in the current role并应用额外的权限限制
$user_permissions = $permissions[$user_role];

$all_permissions = ['view_dashboard', 'edit_settings', 'view_reports', 'delete_post'];

$filtered_permissions = array_filter($all_permissions, function ($permission) use ($user_permissions, $user_restrictions) {
    return in_array($permission, $user_permissions) && !in_array($permission, $user_restrictions);
});

print_r($filtered_permissions);

Output result:

 Array
(
    [0] => view_dashboard
    [2] => view_reports
)

In this example, we not only filter the permissions of the user role, but also further restrict the permissions based on user_restrictions .

summary

array_filter() is a very useful function in PHP, which can help us process and filter arrays efficiently. In permission management, combining array_filter() can easily quickly filter out the required permissions based on different user roles and permission requirements. If you have a large amount of permission data processing in your project, using array_filter() can not only improve the readability of the code, but also improve execution efficiency.