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

Reply via email to