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

```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 "<br>array[".\$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

```