-1

The check_access method below returns true but the check_access_level returns false for both the accountant and the marketer. What could be the problem?

 private static function  check_access_level($value)
    {
        //dd(self::hierarchy_access_subject('accountant'));  //returns false
        //dd(self::hierarchy_access_subject('marketer'));  //returns false
        return self::hierarchy_access_team_leader($value);
}

public static function check_access()
{

   $roles = ['accountant','marketer'];
   return array_walk($roles, 'self::check_access_level');

}
Tim Kariuki
  • 633
  • 8
  • 17

1 Answers1

0

As ryantx comments, array_walk returns true.

If you want to just iterate through roles and check them against a function you could use a foreach with a short-circuit:

<?php
function is_accountant($str)
{
    return $str == 'accountant';
}

function check_roles_for_accountant(array $roles)
{
    foreach($roles as $role)
        if(is_accountant($role))
            return true;

    return false;
}

var_dump(array_walk($a = ['foo'], function($v, $k) {return false; }));
var_dump(check_roles_for_accountant(['accountant', 'marketer']));
var_dump(check_roles_for_accountant(['programmer', 'tester', 'debugger']));

Output:

bool(true)
bool(true)
bool(false)
Progrock
  • 7,373
  • 1
  • 19
  • 25