Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute (set) difference between arrays in PHP?

Tags:

arrays

php

I going to implement ACL (Access control list) in my php website.

In my system user have sum a of roles and permission.

Main algorithm is as follows:

permissions = (permissions_by_role + permission_for_user) - user_banned_permission

so I have three arrays and get those values from database.

For the first part I use this

 $permissions = array_unique(array_merge($permission_by_role, $permission_by_user));

How can I remove my banned permission from permission array? Now I have these two arrays:

$permissions and $permission_banned_for_user[]
like image 309
Rupesh Pawar Avatar asked Dec 10 '25 14:12

Rupesh Pawar


1 Answers

sounds like a perfect use-case for array_diff:

$permissions = array_diff($permissions, $permission_banned_for_user);
like image 163
oezi Avatar answered Dec 12 '25 02:12

oezi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!