SBB
SBB

Reputation: 8970

PHP Function to check value in Array

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;
            }

        }

    }
}

Upvotes: 0

Views: 64

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78994

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;
        }
    }
}

Upvotes: 1

Gershom Maes
Gershom Maes

Reputation: 8140

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;
}

Upvotes: 0

Related Questions