RE: [PHP] How do I convert an array into a mask?
| From: | Ford, Mike [LSS] | Date: | Wed, 06 Nov 2002 15:19:48 +0000 |
| Subject: | RE: [PHP] How do I convert an array into a mask? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-123009@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Daevid Vincent [mailto:daevid@daevid.com]
> Sent: 06 November 2002 12:23
> To: php-general@lists.php.net
>
> 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: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211