Boxerman Posted May 23, 2015 Share Posted May 23, 2015 Hey guys, Hopefully a quick one for you... I'm trying to check if two arrays match then output text. what i have is: if (in_array($usernamelower, $superusers AND $adminusers, true)) { full code: //Let's see who has what access $adminsql = "SELECT * FROM permissions WHERE level = 'Admin' "; $superusersql = "SELECT * FROM permissions WHERE level = 'Super User' "; $adminresult = mysql_query($adminsql) or die('Query failed: ' . mysql_error()); $superuserresult = mysql_query($superusersql) or die('Query failed: ' . mysql_error()); //Let's build the adminuser array while($adminrow = mysql_fetch_assoc($adminresult)){ $adminusers[] =$adminrow['username']; } //Let's build the superuser array while($superuserrow = mysql_fetch_assoc($superuserresult)){ $superusers[] =$superuserrow['username']; } the if(in array) doesnt work can anyone help with this one.. i've tried AND, ||, && Quote Link to comment https://forums.phpfreaks.com/topic/296463-if-2-arrays-are-true/ Share on other sites More sharing options...
Solution jcbones Posted May 23, 2015 Solution Share Posted May 23, 2015 The function in_array doesn't accept multiple arrays.How about a simple: "SELECT username FROM permissions WHERE level = 'Admin' AND level = 'Super User'"; 1 Quote Link to comment https://forums.phpfreaks.com/topic/296463-if-2-arrays-are-true/#findComment-1512516 Share on other sites More sharing options...
Boxerman Posted May 23, 2015 Author Share Posted May 23, 2015 I'm completely stupid... why didn't i.... thank you +1 Quote Link to comment https://forums.phpfreaks.com/topic/296463-if-2-arrays-are-true/#findComment-1512517 Share on other sites More sharing options...
Barand Posted May 23, 2015 Share Posted May 23, 2015 WHERE level = 'Admin' AND level = 'Super User' would require level to have 2 values at the same time ??? Perhaps you mean WHERE level = 'Admin' OR level = 'Super User' Quote Link to comment https://forums.phpfreaks.com/topic/296463-if-2-arrays-are-true/#findComment-1512518 Share on other sites More sharing options...
jcbones Posted May 23, 2015 Share Posted May 23, 2015 Barand, I'm guessing that he has the usernames multiple times in the permissions table, for the different levels. It is a bad design with bad logic, but with the example that the OP put forward, this is how it looks to be. Quote Link to comment https://forums.phpfreaks.com/topic/296463-if-2-arrays-are-true/#findComment-1512522 Share on other sites More sharing options...
Barand Posted May 23, 2015 Share Posted May 23, 2015 Do you mean the table would look like this, where user A is both Admin and Super User? mysql> SELECT * FROM permissions; +----+----------+------------+ | id | username | level | +----+----------+------------+ | 1 | A | Admin | * | 2 | B | Moderator | | 3 | C | Admin | | 4 | D | Super User | | 5 | E | Moderator | | 6 | A | Super User | * +----+----------+------------+ then WHERE level = 'Admin' AND level = 'Super User' will not find any records as there is no single record that has both values at the same time (impossible) mysql> SELECT username -> FROM permissions -> WHERE level = 'Admin' AND level = 'Super User'; Empty set (0.00 sec) Using OR instead of AND would produce mysql> SELECT username -> FROM permissions -> WHERE level = 'Admin' OR level = 'Super User'; +----------+ | username | +----------+ | A | | C | | D | | A | +----------+ However, to find only users with both mysql> SELECT A.username -> FROM permissions A -> INNER JOIN -> permissions B -> ON A.username = B.username -> AND A.level = 'Admin' -> AND B.level='Super User'; +----------+ | username | +----------+ | A | +----------+ Back to the original question. From the two arrays, $admins and $superusers, to find who is in both arrays use array_intersect() $admins = array ('A', 'C'); $superusers = array ('D', 'A'); $both = array_intersect($admins, $superusers); echo '<pre>',print_r($both, true),'</pre>'; /** RESULT ***** Array ( [0] => A ) ****************/ Quote Link to comment https://forums.phpfreaks.com/topic/296463-if-2-arrays-are-true/#findComment-1512524 Share on other sites More sharing options...
QuickOldCar Posted May 23, 2015 Share Posted May 23, 2015 If the levels are a single value do it in one loop and make arrays comparing the levels $adminusers = array(); $superusers = array(); $sql = "SELECT (username,level) FROM permissions WHERE level = 'Admin' OR level = 'Super User' "; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); while($row = mysql_fetch_assoc($result)){ if($row['level'] == 'Admin'){ $adminusers[] = $row['username']; }else{ $superusers[] = $row['username']; } } Or if wanted to get all users privileges $adminusers = array(); $superusers = array(); $users = array(); $sql = "SELECT (username,level) FROM permissions"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); while($row = mysql_fetch_assoc($result)){ if($row['level'] == 'Admin'){ $adminusers[] = $row['username']; }elseif($row['level'] == 'Super User'){ $superusers[] = $row['username']; }else{ $users[] = $row['username']; } } Now this would be ok if wanted a huge list, I personally like to just check what their level is on login. Set in a session if matches admin or superuser. Otherwise no login or not matching those in session would be set as a normal user in session. On the subject of naming levels it's easier to do a numbering one such as 1-10. Quote Link to comment https://forums.phpfreaks.com/topic/296463-if-2-arrays-are-true/#findComment-1512525 Share on other sites More sharing options...
QuickOldCar Posted May 23, 2015 Share Posted May 23, 2015 will not find any records as there is no single record that has both values at the same time (impossible) I thought the same thing Barand, don't see the OP exploding the data. Each appears to have a single value. Quote Link to comment https://forums.phpfreaks.com/topic/296463-if-2-arrays-are-true/#findComment-1512526 Share on other sites More sharing options...
jcbones Posted May 24, 2015 Share Posted May 24, 2015 (edited) You're right Barand, if I have to have an excuse, I'll go with cross-eyed! Edited May 24, 2015 by jcbones Quote Link to comment https://forums.phpfreaks.com/topic/296463-if-2-arrays-are-true/#findComment-1512527 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.