0

On my online shop, there's a form with a big number of text inputs, representing quantities of products. There are three fields for every product: quantity of product in S size, M size and L size.

So the name of input field must contain both the product id and the size. This is simplified code of the form:

foreach ($productid as $id ) {
    echo '<input type="text" name="s'.$id.'" />';
    echo '<input type="text" name="m'.$id.'" />';
    echo '<input type="text" name="l'.$id.'" />';
}

I want to process this input sent via $_POST and save all values of all input field into a single multidimensional array. The format of the desired array is as follows:

$input['32']['m']='20' means that the customer ordered 20x the product of id 32 in size M.

All my tries to do that failed because I don't understand how to loop through $_POST values to turn them into more than a one-dimension array.

Any idea how to process such input and get a two-dimension array?

0

1 Answer 1

1

Brackets [] will make an array, so try this and print_r($_POST) and see:

foreach ($productid as $id ) {
    echo '<input type="text" name="product['.$id.'][s]" />';
    echo '<input type="text" name="product['.$id.'][m]" />';
    echo '<input type="text" name="product['.$id.'][l]" />';
}
Sign up to request clarification or add additional context in comments.

1 Comment

See php.net/manual/en/faq.html.php#faq.html.arrays. Note that this works for PHP, but not for JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.