Skip to content
Advertisement

Recursive Search array with Multiple Needle – PHP

Array :

JavaScript

Function :

JavaScript

Concepts :

Any user get access list to menu, If every user enter to not accessible uri param, decline it.

Example :

User John Doe have list menu

  1. Products (Full Access)
  2. Report (Full Access)
  3. Menu (Read Only)

And John Doe opened https://localhost/dashboard/menu/create , by system decline it.

Expectation :

I want to search value dashboard/menu to get ‘menu_access’ value is R. And validate what if value R in array $dataParams[‘create’] alias [‘F’,’W’] ?

Example :

Use : arrRecursiveAccessMap('dashboard/menu', 'create', $arrayStr);

Expect Result : false

Advertisement

Answer

JavaScript
  • You are repeating the above code snippet in every recursive call which is not needed. You can rather make this as a parameter for your function.

  • You also did return array_merge(array($first_level_key), $callback); but you said Expect Result : false. So, an array_merge is also not needed.

  • You also have this check if (is_numeric($needleOther) || in_array($needleOther, $paramKeys, true)) {, which is again not needed.


Solution:

You can simply make a function, pass the $dataParams and create key as parameters. Keep calling the same function recursively on menu_child and return true if you find the value of menu_access inside $dataParams['create'], else return false.

Snippet:

JavaScript

Online Demo

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