Skip to content

Instantly share code, notes, and snippets.

@awolad
Created March 14, 2019 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awolad/b8ca04787d8ec44a1fc52efb700b9856 to your computer and use it in GitHub Desktop.
Save awolad/b8ca04787d8ec44a1fc52efb700b9856 to your computer and use it in GitHub Desktop.
<?php
// check only array value
function in_array_r($needle, $haystack, $strict = false)
{
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) ||
(is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
// check by array index
function in_array_r_k($needle, $haystack, $keyToCheck, $strict = false)
{
foreach ($haystack as $item) {
if (($strict ? $item[$keyToCheck] === $needle : $item[$keyToCheck] == $needle) ||
(is_array($item) && in_array_r_k($needle, $item, $keyToCheck, $strict))) {
return true;
}
}
return false;
}
$roleMenuPermission = [
['role_menu_permission_id' => 1, 'role_id' => 9, 'menu_id' => 1],
['role_menu_permission_id' => 2, 'role_id' => 2, 'menu_id' => 2],
['role_menu_permission_id' => 3, 'role_id' => 3, 'menu_id' => 4],
];
//echo in_array_r(9, $roleMenuPermission);
echo in_array_r_k(9, $roleMenuPermission, 'menu_id');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment