RE: [PHP] How do I convert an array into a mask?
| From: | Daevid Vincent | Date: | Wed, 06 Nov 2002 09:18:26 +0000 |
| Subject: | RE: [PHP] How do I convert an array into a mask? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-122952@lists.php.net to get a copy of this message | ||
Michael, I like your idea, and had I designed this site, I would have
the bitmask simmilar to what you suggest. However, I'm a contractor
modding an existing/legacy site. Basically the way they have it, is that
each user has a field in the db that is simply a string/mask of which
books a person has access to see. So given the example below, "10110"
means that the person can view books 1, 3, and 4, but not 2 or 5. dig?
Now, I can brute force this, since there are only a few books. That
doesn't lend itself nicely to expansion, but may be my only way. I just
have a gut feeling that it can be automated somehow. To turn 1,3,4 into
10110 seems like there is some 'math' there that can work. I also
thought there might be a built in PHP function that may point me in the
right direction. I'll post my solution when I get it working if it's
elegant...
Thanks anyways.
P.s. thanks for the correction on boolean vs bitwise OR. Duh. I should
have known that ;-)
> -----Original Message-----
> Does anyone know of a nice efficient way to convert an array
> of values into a "mask"...
>
> Here's the deal. Given an array such that:
>
> $purchitem[0] = 1;
> $purchitem[1] = 3;
> $purchitem[2] = 4;
>
> I want to end up with a variable like this:
>
> $mask = "10110";
>
> Additionally, my theory is that then the person purchases
> another book later
>
> $purchitem[0] = 2;
>
> My new mask should be $purchmask = "01000";
>
> Then I can load their previous mask from the database and
> bitwise OR it with the new mask to set the correct permissions. i.e.
>
> $newmask = $mask | $purchmask;
>
> Or ideally = "11110"
>
> Can I boolean OR strings like that in the way I 'hope' it
> will work? Do I need to convert it to an intermediate stage
> or cast it or anything?
>
> Does this make sense? It's for an online book shopping cart.
> I have the reverse working, where I can split the mask into
> what books. And I also have the $purchitem[] working.