Yeak
Yeak

Reputation: 2538

Check for an array's key existing as a another arrays value

I have a array called $menu_array; and currently looks like this

   [0] => Array
    (
        [id_parent_menu] => 4
        [parent_info] => test
        [children_menu] => Array
            (
                [0] => Array
                    (
                        [id_child_menu] => 21
                        [children_info] => test
                    )

                [1] => Array
                    (
                        [id_child_menu] => 22
                        [children_info] => test2.
                    )

            )

    )

and so on.

I also have another array $access that looks like this:

     array(
          [4]='true'
          [22]='true'
     ) 

What I'm trying to do is check to see if the key for the $access array exists as a id_parent_menu, and then put a key and value of

$menu_array[can_view]='true';

Then, also check if inside the $children_menu array inside of the $menu_array if the a $access key exists as a id_child_menu and set a value of can_view = true in there too.

$menu_array['children_menu'][1]['can_view']='true';

Upvotes: 0

Views: 147

Answers (1)

jcjr
jcjr

Reputation: 1503

foreach ($menu_array as $key => $value){
  if(isset($access[$value['id_parent_menu']]) && $access[$value['id_parent_menu']])
  {
    $menu_array[$key]['can_view']=true;

    foreach($value['children_menu']  as $key2 => $value2)
      if(isset($access[$value2['id_child_menu']]) && $access[$value2['id_child_menu']])
         $menu_array[$key]['children_menu'][$key2]['can_view']=true;
  }

}

(A child item can be visible only if the parent item is visible.)

Upvotes: 1

Related Questions