iversonm Posted April 15, 2012 Share Posted April 15, 2012 Basically what I want to do is create an array of inheritance. so here is the levels Owner->Admin->moderator->User->guest owner has all the abilities of admin, moderator, user and guest.. So I want to be able to sort through them and end with an array like this $array['Owner'] = array('Admin', 'Moderator', 'User', 'Guest'); Currently my code works sort of. Basically Admin is only set to inherit Moderator. Moderator only inherits User. So this code works but only to 2 levels. I don't want to add a billion foeachs within each other for each level(currently 4) but I might add more roils so then I would have to add more foreachs. There must be a magic way to do this. Any feedback is appreciated! <?PHP $sql = 'SELECT * FROM '.self::$Config['PERMISSIONS']['Database'].'.permissions_inheritance'; $result = DB::Query($sql); while($row = DB::fetch($result)){ if($row['type'] == '0'){ $group_i[$row['child']][] = $row['parent']; }elseif($row['type'] == '1'){ $user_i[$row['child']][] = $row['parent']; } } //echo count($group_i); print_r($group_i); $groups = array(); foreach($group_i as $Key=>$Value){ foreach($Value as $K=>$V){ $new_array[$Key][] = $V; if(isset($group_i[$V]) && is_array($group_i[$V])){ foreach($group_i[$V] as $Q=>$A){ if(is_array($A)){ foreach($A as $C=>$X){ $new_array[$Key][] = $X; } }else{ $new_array[$Key][] = $A; } } } } } Let me know if you have any questions! I hope you enjoy the challenge lolol Quote Link to comment https://forums.phpfreaks.com/topic/261003-multi-dimensional-arrays/ Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 You could set it to integers instead. For example admin = 100, user = 10, moderator = 50, guest = 5. Whatever the number is, maybe worth avoiding 1 and 0 though, then you can do this: if($power>=50){ admin_or_mod_function(); } if($power>=10){ registered_users_function(); } if($power==100){ admin_function(); } if($power==10){ users_only_function(); } etc It can be saved in the db as a tinyint. You can also create groups, and check if the person is in that group, but usually that's better when it's not checked all the time. Quote Link to comment https://forums.phpfreaks.com/topic/261003-multi-dimensional-arrays/#findComment-1337663 Share on other sites More sharing options...
iversonm Posted April 15, 2012 Author Share Posted April 15, 2012 I can't add to the table permissions_inheritance due to the fact it is used by an external java application. I can add another table for the ranks but for simplicity purposes I would rather not manage that too Quote Link to comment https://forums.phpfreaks.com/topic/261003-multi-dimensional-arrays/#findComment-1337671 Share on other sites More sharing options...
iversonm Posted April 15, 2012 Author Share Posted April 15, 2012 Also I would like to add that at some point someone might make multiple permissions under one group. So Admin inherits Moderator and inherits Side_group. So this won't be a singular chain thus making it more complicated. Quote Link to comment https://forums.phpfreaks.com/topic/261003-multi-dimensional-arrays/#findComment-1337672 Share on other sites More sharing options...
iversonm Posted April 16, 2012 Author Share Posted April 16, 2012 OK so I am one step closed. First step is filtering out duplicates and organizing by level. Essentially sorting by rank without having a column for rank. This way it knows if you don't inherit anyone you are on the bottom of the chain...... $unsorted = $group_i; do{ foreach($unsorted as $Key=>$Value){ if(is_array($Value) && !empty($Value)){ foreach($Value as $K=>$V){ if(!array_key_exists($V, $unsorted)){ $array[] = $V; if(count($Key) === 1 && count($K) === 1 && count($V) === 1 && count($Value) && count($unsorted) === 1){ $array[] = $Key; } if(count($V) === 1){ unset($unsorted[$Key]); } unset($unsorted[$Key][$K]); } } }else{ unset($unsorted[$Key]); } } }while(count($unsorted) > 0); It executes pretty fast and I can't think of a better way. Now just have to loop through the results in this order to figure out the rest. Quote Link to comment https://forums.phpfreaks.com/topic/261003-multi-dimensional-arrays/#findComment-1337678 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.