I am trying to collect data from multiple checkboxes and store them into an array in PHP. I use checklist[] as a name attribute and hidden inputs with value = 0 for unchecked boxes.
<div>
<input type="hidden" name="checklist[]" value="0">
<input type="checkbox" name="checklist[]" value="1">
</div>
Let's say I have three fields. When all boxes are checked, the array stores this:
[0, 1, 0, 1, 0, 1]
If I check only for example the second checkbox I get this:
[0, 0, 1, 0]
And if I check nothing I get three zeroes. Is there any way I can always take 1 for the checked ones and 0 for unchecked ones?
Or I could filter the array afterwards which I tried. The condition for filtering is to drop all zeroes which are immediately followed by ones. That could work but I couldn't figure out how to construct the condition (I tried to use PHP next() function but to no avail.) How can I do this in PHP? Thanks
Aucun commentaire:
Enregistrer un commentaire