获取具有相同名称的所有复选框(已选中或未选中)的值

I am trying to build a form that lists all available items and users can select some items and enter a value to input element aside the checkbox.

Each checkbox has a input text box aside it.

<?php
    foreach ($items as $item):
        $itemID = $item['item_id'];
        $itemTitle = $item['item'];
?>
    <tr>
        <td>
            <li> 
                <div class="checkbox">
                    <input type="hidden" name="selected_items[]" value="0">
                    <input type='checkbox' name='selected_items[]' value='<?php echo $itemID; ?>'/> 
                <?php echo ucfirst($itemTitle); ?>

                </div>
            </li>
        </td>
        <td><input type='number' name='quantities[]' value="0.00" step="0.01" /> </td>
    </tr>
<?php endforeach; ?>

Here's how the form looks:

Available Items    |  Qty
--------------------------
[] Bread           |  [input]
[] Coffee          |  [input]
[] Egg             |  [input]
[] Cake            |  [input]

I want the two arrays : selected_items[] and quantities to be of the same length as they are so I can combine them.

If unchecked, value should be left to 0.

So, combined_array should look like:

0=>0
Coffee=>44
Egg=>56
0=>0

Can anyone help me with this?

Without more detail, this example assumes you want the "combined_array" to set in your $_POST data...

Firstly I would remove the "hidden" selected_items input. As these are an array of inputs, the index keys would be out of alignment.

Setting the index key to a specific value for selected_items, quantities and adding a new input item, should help correlate your $_POST data like so:

<?php foreach ($items as $item):
    $itemID = $item['item_id'];
    $itemTitle = $item['item'];
?>
    <tr>
        <td>
            <li> 
                <div class="checkbox">
                    <input type='checkbox' name='selected_items[<?php echo $itemID; ?>]' value='<?php echo $itemTitle; ?>'/>  <!-- Give your input $itemID key -->
                    <?php echo ucfirst($itemTitle); ?>
                </div>
            </li>
        </td>
        <td><input type='number' name='quantities[<?php echo $itemID; ?>]' value="0.00" step="0.01" /> </td>  <!-- Give your input $itemID key -->
    </tr>
    <input type='hidden' name='available_items[<?php echo $itemID; ?>]' value="<?php echo $itemTitle; ?>" />   <!-- Give your input $itemID value -->
<?php endforeach; ?>


<?php
if(!empty($_POST)){
    foreach($_POST['available_items'] as $itemID=>$itemTitle){ // <-- Loop through your "available_items"
        $itemTitle = (!empty($_POST['selected_items'][$itemID])?$itemTitle:0); // <-- this changes the itemTitle based on whether !empty($_POST['selected_items'][$itemID]) 
        $Qty = (!empty($_POST['selected_items'][$itemID])&&!empty($_POST['quantities'][$itemID])?$_POST['quantities'][$itemID]:0); // <-- Checks for quantities 
        $CombinedArray[$itemTitle] = $Qty;
    }
    print_r($CombinedArray);
}
?>

Your result would be something like this:

"0"=>0
"Coffee"=>44
"Egg"=>56

This is because you have the potential for creating duplicate keys in your $CombinedArray.

Removing this line $itemTitle = (!empty($_POST['selected_items'][$itemID])?$itemTitle:0);:

<?php
if(!empty($_POST)){
    foreach($_POST['available_items'] as $itemID=>$itemTitle){ // <-- Loop through your "available_items"
        $Qty = (!empty($_POST['selected_items'][$itemID])&&!empty($_POST['quantities'][$itemID])?$_POST['quantities'][$itemID]:0); // <-- Checks for quantities 
        $CombinedArray[$itemTitle] = $Qty;
    }
    print_r($CombinedArray);
}
?>

Would result in something like:

"Bread"=>0
"Coffee"=>44
"Egg"=>56
"Cake"=>0