Jump to content

Recommended Posts

Hi there

 

I have two arrays for dealing with permissions, one contains an entry that says a permission is given, but the other one contains and entry that says the same permission is not given. Basically can I run some sort of script that will prioritise one of these entries over the other? It's really hard to give code because loads of the code is complex and irrelevant but if you don't have a clue what I mean I'll supply it.

 

Thanks a lot

Eco

Ok, array 1 is too big to post, it has about 500 entries. However, it's much bigger than array 2.

 

Array 2 is basically taking bits of array 1 and repeating them with the "perm_value" column changed (this is the array I want to override array 1):

Array
(
    [view_forum] => Array
        (
            [0] => Array
                (
                    [perm_name_id] => 1
                    [perm_name] => view_post
                    [perm_group] => 3
                    [perm_id] => 1
                    [perm_user] => 4
                    [forum_id] => 2
                    [perm_value] => 1
                )

        )

    [moderate] => Array
        (
            [0] => Array
                (
                    [perm_name_id] => 2
                    [perm_name] => moderate_post
                    [perm_group] => 2
                    [perm_id] => 2
                    [perm_user] => 4
                    [forum_id] => 2
                    [perm_value] => 1
                )

        )

    [view_hidden_items] => Array
        (
            [0] => Array
                (
                    [perm_name_id] => 3
                    [perm_name] => view_hidden_posts
                    [perm_group] => 3
                    [perm_id] => 3
                    [perm_user] => 4
                    [forum_id] => 2
                    [perm_value] => 1
                )

        )

    [view_hidden_posts] => Array
        (
            [0] => Array
                (
                    [perm_name_id] => 4
                    [perm_name] => view_hidden_pages
                    [perm_group] => 3
                    [perm_id] => 4
                    [perm_user] => 4
                    [forum_id] => 2
                    [perm_value] => 1
                )

        )

    [hide_topic] => Array
        (
            [0] => Array
                (
                    [perm_name_id] => 5
                    [perm_name] => hide_posts
                    [perm_group] => 2
                    [perm_id] => 5
                    [perm_user] => 4
                    [forum_id] => 2
                    [perm_value] => 1
                )

        )

)

 

if the array is a copy of the other with a simple addition of one index then you are repeating yourself - DRY is a fundamental to good programming.

 

I prefer to use bitwise operators in permission systems - what you have is overly complicated IMO

I see a couple possible solutions.

 

1. Utilize array_merge_recursive() on both arrays and create a new array with the second array overwriting the first array values. But, because of the format of your arrays this can't be done. The values will only overwrite if you are not using numerically based indexes. Your inner most values do not have numerically based indexes, but your outer ones do. So, array_merge_recursive() will create new keys to merge the data. Make your arrays like this and you can use array_merge_recursive() and be done with it.

    [view_hidden_posts] => Array
        (
               [perm_name_id] => 4
               [perm_name] => view_hidden_pages
               [perm_group] => 3
               [perm_id] => 4
               [perm_user] => 4
               [forum_id] => 2
               [perm_value] => 1
        )

 

2. Iterrate through all key/value pairs in array 2 and overwrite the values in array one. This will only work if the structure for the two arrays is exactly the same. This will need to be a recursive function.

 

Ok, this function seems to work. It will overwrite the original array with new values, so if you need to maintain the original values make a copy before running it.

 

function overwriteArray(&$originalAry, $newAry)
{
    foreach ($newAry as $key => $value)
    {
        if (is_array($value))
        {
            overwriteArray($originalAry[$key], $newAry[$key]);
        }
        else
        {
            $originalAry[$key] = $value;
        }
    }
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.