[PHP] In_array Help

Master Summoner
Joined
May 11, 2008
Messages
513
Reaction score
99
Hello, I'm currently making a user system with a navigation which requries different ranks, such as news (rank 3) events (rank 4)

but the prob is you could just be a events manager and not need news so > is out of the question.

So the ranking system in the database look like this

3|4|5 == admin
3 = news
3|4 = news and events and so on..

Code:
PHP:
<?
// _base.php
function allowrank($rank,$catrank)
{
        $tmp = explode('|', $rank);
        if(in_array($catrank,$tmp))
        {
            return true;
        }
        else
        {
            return false;
        }
}



// navigation.php 


$sql = mysql_query("SELECT * FROM navigation WHERE main='1' ORDER by catid");
    while($row = mysql_fetch_assoc($sql)){ $i++; 
        $sql2 = mysql_query("SELECT * FROM navigation WHERE main='0' AND catid='".$row['catid']."'");
        if(allowrank($rank,$row['minrank'])) 
        {
            $rank = 0;    
        }
        else
        {
            $ranks = $rank;
        }
        if($ranks != $rank)
        {
    ?>
                
                <div class="<?php echo $row['div']; ?>" onclick="SwitchMenu('sub<?php echo $i; ?>')"></div>
                <span class="submenu" id="sub<?php echo $i; ?>">
                <?php while($row = mysql_fetch_assoc($sql2)){ 
                    if($_GET['page'] == $row['link']){ ?>            
                    <b><a href="./?page=<?php echo $row['link']; ?>"><?php echo $row['catname']; ?></a></b><?php echo space(1); ?>
                    <? }else{ ?>
                    <a href="./?page=<?php echo $row['link']; ?>"><?php echo $row['catname']; ?></a><?php echo space(1); ?>
                <? }} ?>
                </span>            
    <? }} ?>
Btw these are snipets of coding added together.

I've never used in_array before, and I'm not 100% sure how to use it...

Many thanks, James.
 
in_array() simply is what it says that it is: Is something in a array

For example:
PHP:
$array = array('a', 'b', 'c');
if(in_array('d', $array)){
echo 'We have d in our array'; // Will never be printed out, becuase there is no d in our array
}
if(in_array('a', $array)){
echo 'We have a in our array'; // Will be printed out becuase a is a value in our array
}
 
Last edited:
What you should be looking at is bitmasks - much, much faster and shorter. Consider the binary representations of your levels:
Code:
1  = 0001
2  = 0010
4  = 0100
8  = 1000

Now say you wish to grand someone rank 1 and 4:
PHP:
$rank = 1 | 4; // Set all bits that are on in 1 or 4.

echo $rank; // Outputs 5, which is 0101 in binary
It really is that easy! Also check out the page on bitwise operators in the manual.

The beauty is, you can also do this already in your SQL query:
Code:
SELECT  `username`
FROM    `accounts`
WHERE  (`accessMask` & 4)
(This selects someone who will at least have access mask 4).

Little more complicated: someone who is both an access mask 4 and mask 2:
Code:
SELECT  `username`
FROM    `accounts`
WHERE  (`accessMask` & (4 | 2))

Or the other way around, someone who does not have access right 8:
Code:
SELECT  `username`
FROM    `accounts`
WHERE  ((`accessMask` ^ 8) & 8)

The adventage over what you are doing is that you can suffice with a single bitwise operation, which is much, much faster than several string comparisons. Additionally, you can simply enforce access rights directly from your database instead of having a PHP script parse them.

On a sidenote: for added readability you should define the various access masks as a class constant:
PHP:
class Login {
  const   ACCESS_READ   = 1;
  const   ACCESS_NEWS   = 2;
  const   ACCESS_ADMIN  = 4;
  
  public function __construct () {
    $userMask = self::ACCESS_READ | self::ACCESS_NEWS;
  }
}
 
What you should be looking at is bitmasks - much, much faster and shorter. Consider the binary representations of your levels:
Code:
1  = 0001
2  = 0010
4  = 0100
8  = 1000
Now say you wish to grand someone rank 1 and 4:
PHP:
$rank = 1 | 4; // Set all bits that are on in 1 or 4.

echo $rank; // Outputs 5, which is 0101 in binary
It really is that easy! Also check out the page on bitwise operators in the manual.

The beauty is, you can also do this already in your SQL query:
Code:
SELECT  `username`
FROM    `accounts`
WHERE  (`accessMask` & 4)
(This selects someone who will at least have access mask 4).

Little more complicated: someone who is both an access mask 4 and mask 2:
Code:
SELECT  `username`
FROM    `accounts`
WHERE  (`accessMask` & (4 | 2))
Or the other way around, someone who does not have access right 8:
Code:
SELECT  `username`
FROM    `accounts`
WHERE  ((`accessMask` ^ 8) & 8)
The adventage over what you are doing is that you can suffice with a single bitwise operation, which is much, much faster than several string comparisons. Additionally, you can simply enforce access rights directly from your database instead of having a PHP script parse them.

On a sidenote: for added readability you should define the various access masks as a class constant:
PHP:
class Login {
  const   ACCESS_READ   = 1;
  const   ACCESS_NEWS   = 2;
  const   ACCESS_ADMIN  = 4;
  
  public function __construct () {
    $userMask = self::ACCESS_READ | self::ACCESS_NEWS;
  }
}
Sorry, I really don't understand it, could you give me indepth theory
 
I have added some examples to my post about this, these should make it a bit more clear.

Keep in mind that bitwise operations can be a bit confusing at first - I know they were for me. You should be able to see the logic behind them fairly quickly though if you just fiddle around with them for a bit.
 
Last edited:
Back