0

I am trying to come up with a function in order to get me some specific data from an array. It's part of a permissions system I am doing for a page.

Here is the array of data I have:

[views] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [permItem] => viewOriginalFeedback
                        [actions] => SimpleXMLElement Object
                            (
                                [view] => 0
                                [edit] => 0
                            )

                    )

                [1] => SimpleXMLElement Object
                    (
                        [permItem] => viewTarget
                        [actions] => SimpleXMLElement Object
                            (
                                [view] => 0
                                [edit] => 0
                            )

                    )

            )

    )

What I am trying to do is create a function where I can pass is pass it a perm item and action like so. if(myFunction('viewOriginalFeedback', 'view')) if this returns 1 I show the content, if its a 0 then I know to not show it.

The issue is that these views may not exist at all in the array so in that case it would be false as if the action was set to 0.

I was playing around with something like this but I feel like there is a more elegant way to complete it.

// Permission Check
function checkPerm($permItem, $action){
    foreach($permissions->data->views as $view){

        if($view->permItem == $permItem){

            if($view->actions == $action){
                    return $view->actions->$action;
            }

        }

    }
}
2
  • You probably want if(isset($view->actions->$action)) and I would probably go for an associative array instead of array of objects. Commented Jan 29, 2015 at 18:15
  • you're code looks fine, what's the problem? Commented Jan 29, 2015 at 19:01

2 Answers 2

1

It would be simpler and not require looping to use an associative array:

Array
(
    [viewOriginalFeedback] => Array
        (
            [view] => 0
            [edit] => 0
        )

    [viewTarget] => Array
        (
            [view] => 0
            [edit] => 0
        )

)

Then it's as simple as:

if(isset($permissions[$permItem][$action])) {
    return (bool)$permissions[$permItem][$action];
}
return false;

You could maintain the use of objects and still keep it simple with a string key:

[viewOriginalFeedback] => SimpleXMLElement Object
                (
                    [permItem] => viewOriginalFeedback
                    [actions] => SimpleXMLElement Object
                        (
                            [view] => 0
                            [edit] => 0
                        )

                )

With the same check:

if(isset($permissions[$permItem]->actions->$action)) {
    return (bool)$permissions[$permItem]->actions->$action;
}
return false;

With your current array of objects your current approach is probably the way to go, but more like:

foreach($permissions->data->views as $view){
    if($view->permItem == $permItem){
        if(isset($view->actions->$action)){
            return (bool)$view->actions->$action;
        } else {
            return false;
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the reply. It seems like this would be the best way to go. However, I am trying to look at modifying my SQL to provide the needed format for this. I use TSQL and output it as XML so I need to get the format correct.
Added a case for your current array of objects.
Thanks, I tried the example and the array contains 0 for the value of edit but the output is 1.
0

I'm just taking advantage of array_filter to shorten the code. This assumes that you can't convert to using an associative array like AbraCadaver suggested. It's also slightly less efficient than your own code because array_filter will walk the rest of the array, even after it has found the first match.

function checkPerm($permItem, $action){
    $val = array_filter($permissions->data->views, function($v) {
        return $v->permItem === $permItem;
    });
    return isset($vals[0]->$action) && (bool) $vals[0]->$action;
}

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.