RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread Daevid Vincent
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 = 0
 
 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.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread Ernest E Vogelsinger
At 10:18 06.11.2002, Daevid Vincent said:
[snip]
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
[snip] 

It's quite easy using the left-shift operator . Note that the function
below only works for values 1-31 on 32bit systems.

?php

function makebits($array)
{
$result = 0;
foreach ($array as $seq) {
if (is_numeric($seq)) {
$i = 1  ($seq-1); // assuming ID's are 1-based
$result += $i;
}
}
return $result;
}

$a = array(1,3,5,9);// 1 0001 0101 = 0x115
echo 'xmp', sprintf('0x%x', makebits($a)), \n/xmp;

?

-- 
   O Ernest E. Vogelsinger
   (\)ICQ #13394035
^ http://www.vogelsinger.at/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread John W. Holmes
 So given the example below, 10110
 means that the person can view books 1, 3, and 4, but not 2 or 5. dig?

Explain that to me... I know binary, but I can't see how that equates to
1, 3, and 4.

---John Holmes...



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread Jon Haworth
Hi John,

  So given the example below, 10110
  means that the person can view books 1, 3, and 4, 
  but not 2 or 5. dig?
 
 Explain that to me... I know binary, but I can't see 
 how that equates to 1, 3, and 4.

Because you know binary :-)

The above is a series of yes/no flags, not a binary number. Reading from
left to right:

book 1 : yes
book 2 : no
book 3 : yes
book 4 : yes
book 5 : no

Cheers
Jon

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread John W. Holmes
 Hi John,
 
   So given the example below, 10110
   means that the person can view books 1, 3, and 4,
   but not 2 or 5. dig?
 
  Explain that to me... I know binary, but I can't see
  how that equates to 1, 3, and 4.
 
 Because you know binary :-)
 
 The above is a series of yes/no flags, not a binary number. Reading
from
 left to right:
 
 book 1 : yes
 book 2 : no
 book 3 : yes
 book 4 : yes
 book 5 : no
 
 Cheers
 Jon

Ok, so knowing binary and now knowing that, :)

Couldn't you just treat the number as a string and tear it apart to see
what permissions the user has?

?

$var = 10110;

$l = strlen($var);

for($x=0;$x$l;$x++)
{
  if($var{$x}) 
  { echo Permission for book $x is goodbr\n; }
  else
  { echo Permission for book $x is badbr\n; }
}

?

---John Holmes...



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread Daevid Vincent
That's the EASY part John!

The hard part is converting the array (which was a checkbox array from a
form submission) into the binary string (as per the original post)

 Here's the deal. Given an array such that:
 $purchitem[0] = 1;  //purchased book #1 checkbox enabled
 $purchitem[1] = 3;  //purchased book #3 checkbox enabled
 $purchitem[2] = 4;  //purchased book #4 checkbox enabled
 I want to end up with a variable like this:
 $mask = 10110;

Get it now? ;-)
 

 -Original Message-
 From: John W. Holmes [mailto:holmes072000;charter.net] 
 Sent: Wednesday, November 06, 2002 4:19 AM
 To: 'Jon Haworth'; [EMAIL PROTECTED]
 Subject: RE: [PHP] How do I convert an array into a mask?
 
 
  Hi John,
  
So given the example below, 10110
means that the person can view books 1, 3, and 4,
but not 2 or 5. dig?
  
   Explain that to me... I know binary, but I can't see
   how that equates to 1, 3, and 4.
  
  Because you know binary :-)
  
  The above is a series of yes/no flags, not a binary number. Reading
 from
  left to right:
  
  book 1 : yes
  book 2 : no
  book 3 : yes
  book 4 : yes
  book 5 : no
  
  Cheers
  Jon
 
 Ok, so knowing binary and now knowing that, :)
 
 Couldn't you just treat the number as a string and tear it 
 apart to see what permissions the user has?
 
 ?
 
 $var = 10110;
 
 $l = strlen($var);
 
 for($x=0;$x$l;$x++)
 {
   if($var{$x}) 
   { echo Permission for book $x is goodbr\n; }
   else
   { echo Permission for book $x is badbr\n; }
 }
 
 ?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread John W. Holmes
 That's the EASY part John!
 
 The hard part is converting the array (which was a checkbox array from
a
 form submission) into the binary string (as per the original post)
 
  Here's the deal. Given an array such that:
  $purchitem[0] = 1;  //purchased book #1 checkbox enabled
  $purchitem[1] = 3;  //purchased book #3 checkbox enabled
  $purchitem[2] = 4;  //purchased book #4 checkbox enabled
  I want to end up with a variable like this:
  $mask = 10110;
 
 Get it now? ;-)

Got it...

Are there always going to be X digits in the code? If so, this would
work.

?
$purchitem[0] = 1;
$purchitem[1] = 3;
$purchitem[2] = 4;  


$mask = '';
$length = 5;
$y = 0;

for($x=1;$x$length+1;$x++)
{
  if($purchitem[$y] == $x)
  { 
$mask .= '1'; 
$y++;
  }
  else
  { $mask .= '0'; }
}

echo $mask;

?

---John Holmes...  





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread Daevid Vincent
Ernest, close but you have it reversed for my needs (i.e. true binary
form). Notice that my LSB is to the left, not right.

As Evan Nemerson pointed out to me, the decbin(), bindec(), Dec2Bin()
and Bin2Dec() functions shed some light. What I need is the equivillant
functions but with a way to flip the order of the bits from LSB to MSB.
I believe that is called little endian and big endian?

I've been playing with one function to convert my purchitem[] to a
decimal value, then use the decbin() to get a string, then reverse it...
Seems cumbersome but mebbe that's the solution?

function Array2Dec($array)
{
$decimal = 0;
while ( list($Key, $Val) = each($array) ) 
{  
  //notice the ($Val - 1) because 
//our array starts at index 0, not 1
$decBit = pow(2, ($Val - 1));
$decimal += $decBit;
echo brarray[.$Key.] = .$Val. :: .$decBit;
} 
return $decimal;
}

function reverseString($myString)
{
$charArray = preg_split('//', $myString, -1,
PREG_SPLIT_NO_EMPTY);
$revchars = array_reverse($charArray);
$revString = ;
while ( list($Key, $Val) = each($revchars) ) { $revString .=
$Val; }
return $revString;
}


 -Original Message-
 From: Ernest E Vogelsinger [mailto:ernest;vogelsinger.at] 
 Sent: Wednesday, November 06, 2002 1:28 AM
 To: [EMAIL PROTECTED]
 Subject: RE: [PHP] How do I convert an array into a mask?
 
 
 At 10:18 06.11.2002, Daevid Vincent said:
 [snip]
 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
 [snip] 
 
 It's quite easy using the left-shift operator . Note that 
 the function below only works for values 1-31 on 32bit systems.
 
 ?php
 
 function makebits($array)
 {
 $result = 0;
 foreach ($array as $seq) {
 if (is_numeric($seq)) {
 $i = 1  ($seq-1); 
 // assuming ID's are 1-based
 $result += $i;
 }
 }
 return $result;
 }
 
 $a = array(1,3,5,9);// 0001 0001 0101 = 0x115
 echo 'xmp', sprintf('0x%x', makebits($a)), \n/xmp;
 
 ?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread Daevid Vincent
Oooh! I think you're on to something there. Nice!

Hey, what's the  symbol for? I see in the manual the  is a
reference (like a pointer in C I assume), but I can't find the 
explained.

   if($purchitem[$y] == $x)


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread John W. Holmes
 Oooh! I think you're on to something there. Nice!
 
 Hey, what's the  symbol for? I see in the manual the  is a
 reference (like a pointer in C I assume), but I can't find the 
 explained.
 
if($purchitem[$y] == $x)

It'll suppress warnings and errors. If the $purchitem does not have a
key 4 or 5, like in your example, then you'll get a warning for
undefined offset, depending on your error reporting level. This just
suppresses that. 

If you do it this way, you may want to sort $purchitem if you can't
guarantee it's going to be in ascending order. Or you can use in_array()
and not care about the order.

---John Holmes...



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread Ernest E Vogelsinger
At 13:43 06.11.2002, Daevid Vincent said:
[snip]
Ernest, close but you have it reversed for my needs (i.e. true binary
form). Notice that my LSB is to the left, not right.

Ah, ic, you want a string like 1100101 - that's easy :)
Note that I do _not_ pass the array by reference here since I modify it
(using asort).

function make_a_special_bitlike_string($array)
{
$result = '';
sort($array, SORT_NUMERIC);
$max = $array[count($array)-1];
for ($i = 1; $i = $max; ++$i) {
if (in_array($i, $array))
$result .= '1';
else
$result .= '0';
   }
   return $result;
}

if you need this as a number, simply return (int)$result, but beware that
this breaks if there are more than 32 digits (highest value  32).


-- 
   O Ernest E. Vogelsinger
   (\)ICQ #13394035
^ http://www.vogelsinger.at/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How do I convert an array into a mask?

2002-11-06 Thread Tom Rogers
Hi,

Wednesday, November 6, 2002, 2:56:33 PM, you wrote:
DV Does anyone know of a nice efficient way to convert an array of values
DV into a mask...

DV Here's the deal. Given an array such that:

DV $purchitem[0] = 1;
DV $purchitem[1] = 3;
DV $purchitem[2] = 4;

DV I want to end up with a variable like this:

DV $mask = 10110;

DV Additionally, my theory is that then the person purchases another book
DV later

DV $purchitem[0] = 2;

DV My new mask should be $purchmask = 01000;

DV Then I can load their previous mask from the database and boolean OR it
DV with the new mask to set the correct permissions. i.e.

DV $newmask = $mask | $purchmask;

DV Or ideally = 0

DV Can I boolean OR strings like that in the way I 'hope' it will work? Do
DV I need to convert it to an intermediate stage or cast it or anything?

DV Does this make sense? It's for an online book shopping cart. I have the
DV reverse working, where I can split the mask into what books. And I also
DV have the $purchitem[] working.

This should do it:


$purchitem[0] = 1;
$purchitem[1] = 3;
$purchitem[2] = 4;

$s = '0';
while(list($key,$val) = each($purchitem)){
$s[$val-1] = '1';
}
echo s = $s br;

-- 
regards,
Tom


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] How do I convert an array into a mask?

2002-11-06 Thread Ford, Mike [LSS]
 -Original Message-
 From: Daevid Vincent [mailto:daevid;daevid.com]
 Sent: 06 November 2002 12:23
 To: [EMAIL PROTECTED]
 
 That's the EASY part John!
 
 The hard part is converting the array (which was a checkbox 
 array from a
 form submission) into the binary string (as per the original post)
 
  Here's the deal. Given an array such that:
  $purchitem[0] = 1;  //purchased book #1 checkbox enabled
  $purchitem[1] = 3;  //purchased book #3 checkbox enabled
  $purchitem[2] = 4;  //purchased book #4 checkbox enabled
  I want to end up with a variable like this:
  $mask = 10110;
 
 Get it now? ;-)

Nah, that's dead easy:

   $mask = str_repeat(0, number_of_books_to_be_tracked);

   foreach($purchitem as $book_no):
  $mask{$book_no} = 1;
   endforeach;

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] How do I convert an array into a mask?

2002-11-05 Thread Daevid Vincent
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 boolean OR it
with the new mask to set the correct permissions. i.e.

$newmask = $mask | $purchmask;

Or ideally = 0

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.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How do I convert an array into a mask?

2002-11-05 Thread rija
Why don't you ask Jim Carrey ???
He knew more that whoever here about The MASK !!!

Good luck.
- Original Message - 
From: Daevid Vincent [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Wednesday, November 06, 2002 3:56 PM
Subject: [PHP] How do I convert an array into a mask?


 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 boolean OR it
 with the new mask to set the correct permissions. i.e.
 
 $newmask = $mask | $purchmask;
 
 Or ideally = 0
 
 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.
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How do I convert an array into a mask?

2002-11-05 Thread Michael Sims
On Tue, 5 Nov 2002 20:56:33 -0800, you wrote:

Does anyone know of a nice efficient way to convert an array of values
into a mask...

I'm going to assume that you mean a bitmask.  I'm not exactly sure
what you're trying to accomplish, so I may be off base here, but let
me describe how I'm using bitmasks in one of my applications:

I assist in development of a real estate web site where end users can
enter search criteria and get a list of properties.  One of the search
criteria is listing type (as in residential, land, etc.).  In the
search form we allow people to search for multiple listing types.
There are six types altogether, and the user could select none of
them, all of them, or any combination in between.  For reasons I won't
bother to go into here, I needed to represent these choices in the
most efficient way possible so I could pass the data as one value in a
query string.  Rather than pass the choices as an array, I generate a
bitmask of the selection and then use PHP's bitwise operators to
reconstruct the array when I need to (more on this below).

Then I can load their previous mask from the database and boolean OR it
with the new mask to set the correct permissions. i.e.

$newmask = $mask | $purchmask;

That's not a boolean OR.  Boolean OR is '||', what you're talking
about is a bitwise OR:

http://www.php.net/manual/en/language.operators.bitwise.php

This is how I do it.  First of all I define an array in a config file
that enumerates all of my listing types.  I then assign integer values
to each type:

$listingTypes = array(
  'Residential/Single Family' = 1,
  'Lots, Land  Farm' = 2,
  'Commercial Land'   = 4,
  'Commercial/Industrial' = 8,
  'Condos/Townhouses' = 16,
  'Multi-Family'  = 32
);

To construct the bitmask value, I take the integer values of each type
that is selected and I add them together to get a bitmask value.  For
example, if someone chooses both Commercial Land and
Commercial/Industrial, the bitmask value is 12.

After I have this value I can retrieve the individual types by looping
through the listing type array and doing a bitwise AND with the
bitmask.  If the result is true (i.e. 1) then I know that particular
listing type was selected:

foreach ($listingTypes as $listingType = $bit) {
  if ($bit  $listingTypeBitmask) {
echo $listingType was selected.;
  }
}

Basically I never deal with the actual binary values...I store and
manipulate everything in decimal, and let PHP do the work using the
bitwise operators.  BTW, I got the this idea from the way that PHP's
error reporting level is configured...each error type (such as
E_USER_NOTICE, etc.) has an integer equivalent and PHP tracks which
error types are to be reported as a bitmask, similar to the above...

I know this probably isn't quite what you're trying to accomplish, but
maybe it can help give you some ideas...

HTH

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php