1

I have a multidimensional associative array. I need to loop through the array and put one value through another function that returns a Boolean that will be the value of another member in the array. See below.

The multidimensional array:

$grouparray = array(
    "428995" => array(
        "group" => "Meetings In Camera - Read",
        "access" => false
    ),
    "896325" => array(
        "group" => "Meetings In Camera - Modify",
        "access" => false
    ),
    "485563" => array(
        "group" => "Security Meetings - Modify",
        "access" => false
    ),
    "556321" => array(
        "group" => "TAC Meetings - Modify",
        "access" => false
    ),
    "658823" => array(
        "group" => "Restricted Meeting - Modify",
        "access" => false
    ),
    "985465" => array(
        "group" => "Admin Meetings - Modify",
        "access" => false
    ),
);

I have a function that queries the ldap and returns true or false. I tried nested for loops but the access values are not being changed. However, if I run them individually it works as expected.

This works (But then have to do for each item in array):

checkGroupMembership($ldap, $user, $grouparray[428995]['group']);

However, this does not work

function groupSearch($ldapconn, $user, $grouparray) {
    foreach ($grouparray as $key => $value) {
        foreach ($value as $sub_key => $sub_value) {
            $grouparray[$sub_value][access] = checkGroupMembership($ldap, $user, $grouparray[$sub_value]['group]); // this returns true or false
        }
    }
}

So basically I want to loop through array use 'group' as an input to another function that returns a Boolean to 'access'

0

1 Answer 1

1

Your first foreach() is iterating through the top level of the array, and then your second foreach() is iterating through the second level. Each second level contains "group" and "access".

You only need a single loop;

Also since you're using a function the scope of any changes will be limited to the function, so you should return $grouparray from the function to then check it for changes.

function groupSearch($ldapconn, $user, $innergrouparray) {
    foreach ($innergrouparray as $key => $value) {
        $innergrouparray[$key]['access'] = checkGroupMembership($ldap, $user, $value['group']); // this returns true or false
    }

return $innergrouparray;
}

$newgrouparray = groupSearch($ldap, $user, $grouparray);
print_r($newgrouparray);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. Not enough rep to upvote. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.