Skip to content
Advertisement

Check if any item in one array is contained in any item in another array

I have an array of subscriptions/plans (access levels):

JavaScript

A user can be subscribed to one or more plans.

JavaScript

A ‘process’ requires a certain subecription plan level – or multiple plan levels:

JavaScript

I want to check if $user_one has the ‘authority’ to access $process_1, and separately check if $user_one has authority to access $process_2 . And do the same check for $user_two. (“Authority” is if the user has the subscription plan required by the process.)

It looks like I need to check if any user plan subscription (one or more) is contained in the subscription requirements of the processes.

I tried using bitwise checking (which is why the PLANs have binary values), but that will only work for the check of $process_1 . How do I check if $user_1 has access to $process_2 ? Or, how to check if any value of the user array is contained in any value of the process requirement array?

Advertisement

Answer

Since one of the users (jirarium) copies my comment and posts it as an answer, I might as well do it myself and actually get the credit for my piece of code, and not let someone who just copies and pastes other peoples codes claim it as his own.

Here is the function I made yesterday. You input the user & process; It will then iterate through all of the users values (or plans in this case) and check if any one of them is contained in the array of process values you included. Basically check if any value of the user array is contained in any value of the process requirement array, exactly what you wanted.

It will return a true or false that can be used in an if statement

JavaScript

Usage:

JavaScript

Live Demo: http://sandbox.onlinephpfunctions.com/code/83d8d135b03d584713738b56db40ecc21e9f4ddd

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement