Jump to content

multi dimensional arrays


iversonm

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/261003-multi-dimensional-arrays/
Share on other sites

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.