Current Location: Home> Latest Articles> Use array_diff() to build a differentiated menu permission system

Use array_diff() to build a differentiated menu permission system

M66 2025-06-06

Menu permission management is an important part of this when developing some role-based permission management systems. In order to enable different users or roles to access different menu items, we can use PHP's built-in function array_diff() to compare and filter out menu items that users can access. This article will introduce how to use the array_diff() function to implement a differentiated menu permission management system.

1. Introduction to array_diff() function

In PHP, the array_diff() function can be used to compare two or more arrays, returning elements that exist in the first array but are not in other arrays. Its basic usage is as follows:

 array_diff(array $array1, array $array2, array ...$arrays): array

This function returns an array containing elements in $array1 but not in $array2 and other arrays.

2. Basic ideas for permission management

In menu permission management, each user has a set of permission identifiers (usually the ID of the menu item), and the system decides which menus to display based on the user's role. We can use the array_diff() function to make a difference between the administrator's complete menu and the menu of ordinary users, thereby obtaining differentiated menu permissions.

3. Implementation steps

3.1 Define menus and user permissions

First, we define an array containing all menu items and an array of permissions for each user. For example, suppose we have a system with multiple menu items, each menu item has a unique ID.

 // All menu items in the system
$all_menus = [
    1 => 'front page',
    2 => 'User Management',
    3 => 'Product Management',
    4 => 'Order Management',
    5 => 'Financial statements',
    6 => 'System Settings'
];

// Administrator's menu permissions
$admin_permissions = [1, 2, 3, 4, 5, 6];  // Administrators can access all menus

// Menu permissions for ordinary users
$user_permissions = [1, 2, 3, 4];  // Ordinary users can only access some menus

3.2 Use array_diff() to calculate permission differences

Next, we use array_diff() to get the differentiated part of the administrator menu and the ordinary user menu. The menu we want to return is a menu item that ordinary users do not have permission to access, that is, a menu item that administrators have but ordinary users do not have.

 // Get the difference between administrator and normal user menus
$diff_permissions = array_diff($admin_permissions, $user_permissions);

// Print out differentiated menu permissions
echo "Administrator-exclusive menu permissions:\n";
foreach ($diff_permissions as $menu_id) {
    echo $all_menus[$menu_id] . "\n";
}

3.3 Output results

Through the above code, we can output menu items that the administrator has but cannot access to ordinary users. The execution results are as follows:

 Administrator-exclusive menu permissions:
Financial statements
System Settings

4. Use URL to implement permission control

In the actual permission management system, we also need to dynamically display different menu links based on the user's permissions. Assuming that the menu item needs to be linked to a specific URL, we can include the URL in the menu array and filter the URL at the same time when permissions are filtered.

 // Definition tape URL Menu Items
$menus_with_url = [
    1 => ['name' => 'front page', 'url' => 'https://www.m66.net/home'],
    2 => ['name' => 'User Management', 'url' => 'https://www.m66.net/user-management'],
    3 => ['name' => 'Product Management', 'url' => 'https://www.m66.net/product-management'],
    4 => ['name' => 'Order Management', 'url' => 'https://www.m66.net/order-management'],
    5 => ['name' => 'Financial statements', 'url' => 'https://www.m66.net/financial-report'],
    6 => ['name' => 'System Settings', 'url' => 'https://www.m66.net/system-settings']
];

// Get the difference between administrator and normal user menus
$diff_permissions = array_diff($admin_permissions, $user_permissions);

// Print differentiated menus and their URL
echo "Administrator-exclusive menu permissions及链接:\n";
foreach ($diff_permissions as $menu_id) {
    echo $menus_with_url[$menu_id]['name'] . " - " . $menus_with_url[$menu_id]['url'] . "\n";
}

The execution results will look like this:

 Administrator-exclusive menu permissions及链接:
Financial statements - https://www.m66.net/financial-report
System Settings - https://www.m66.net/system-settings

5. Summary

Through the above method, we use PHP's array_diff() function to implement a differentiated menu permission management system. Administrators can access all menus, while ordinary users can only access authorized menus. Combined with the URL, we can provide access links for each menu item to further enhance the user experience.

In this way, we can not only dynamically display menus that users have permission to access, but also effectively control which menu items are visible to specific users. You can expand this function according to actual needs and add more permission control logic.