ecopetition Posted August 23, 2009 Share Posted August 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/171554-return-false-even-if-something-else-wants-to-return-true/ Share on other sites More sharing options...
Mark Baker Posted August 23, 2009 Share Posted August 23, 2009 So what do these arrays actually look like? Quote Link to comment https://forums.phpfreaks.com/topic/171554-return-false-even-if-something-else-wants-to-return-true/#findComment-904703 Share on other sites More sharing options...
ecopetition Posted August 23, 2009 Author Share Posted August 23, 2009 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 ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/171554-return-false-even-if-something-else-wants-to-return-true/#findComment-904707 Share on other sites More sharing options...
ecopetition Posted August 24, 2009 Author Share Posted August 24, 2009 Hi guys, I don't mean to sound impatient but if anyone is online who can, can I politely request that they help me? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/171554-return-false-even-if-something-else-wants-to-return-true/#findComment-905045 Share on other sites More sharing options...
ToonMariner Posted August 24, 2009 Share Posted August 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/171554-return-false-even-if-something-else-wants-to-return-true/#findComment-905050 Share on other sites More sharing options...
Psycho Posted August 24, 2009 Share Posted August 24, 2009 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; } } } Quote Link to comment https://forums.phpfreaks.com/topic/171554-return-false-even-if-something-else-wants-to-return-true/#findComment-905068 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.