0

I have a method with reference in param. But when I'm trying to do recursive call, no values are saved to my array, although there should be 2 values in array. Here is my function, the problem is, when it's called again while checking $nodesArray[$nodeKey]['nodes']. That should check menu sub-nodes and give back that nodes, which the selected role can see. At the end of every foreach loop, I use dump($nodesArray). After last loop when checking subnodes, array has only the right values. But when it gets back to "main" loop, value of ['nodes'] is NULL. What could be wrong? Thank you!

private function filterMenuNodesByRole(& $nodesArray)
{
    foreach ($nodesArray as $nodeKey => $nodeItem) {
        $link = trim($nodeItem['href'], self::SEPARATOR_PRESENTER_LINK);

        if ((substr($link, -1) == '!') && ($this->user->isInRole(AuthRoleEnum::SUPERADMIN) === false)) {
            unset($nodesArray[$nodeKey]);
        }

        if (substr_count($link, self::SEPARATOR_PRESENTER_LINK) == 2) {
            $resource = substr($link, 0, strrpos($link, self::SEPARATOR_PRESENTER_LINK));
            $privilege = substr($link, strrpos($link, self::SEPARATOR_PRESENTER_LINK) + 1);

            if ($this->user->isAllowed($resource, $privilege) === false) {
                unset($nodesArray[$nodeKey]);
            } else if (!empty($nodeItem['nodes'])) {
                $nodesArray[$nodeKey]['nodes'] = $this->filterMenuNodesByRole($nodeItem['nodes']);
            }
        } else {
            if (!empty($nodeItem['nodes'])) {
                $nodesArray[$nodeKey]['nodes'] = $this->filterMenuNodesByRole($nodeItem['nodes']);
            }
        }

        if (isset($nodeItem['nodes']) && count($nodeItem['nodes']) == 0) {
            unset($nodesArray[$nodeKey]);
            continue;
        }
        dump($nodesArray);
    }
}

1 Answer 1

1

you are assigning a return value to 'nodes', but the function doesn't return anything -> which results in the NULL.

$nodesArray[$nodeKey]['nodes'] = $this->filterMenuNodesByRole($nodeItem['nodes']); should be $this->filterMenuNodesByRole($nodeItem['nodes']);

but when you run the code now, it seems like the results of the recursion are ignored. The solution for this is to change the way your foreach works

foreach ($nodesArray as $nodeKey => & $nodeItem) {

this way, any changes to $nodeItem wil be persistent.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.