0

I have a form where the user can add all the additional fields that they need.

Note: I would like to find a way to organize the code more efficiently, I will explain it in detail later.

enter image description here

As it is structured in HTML, I have simplified it to make it easier to understand:

<form action="" method="post">
    <h1>Products</h1>       
    <p>
        <input type="text" name="product_name[]" value="Product #1">
        <input type="text" name="product_sku[]" value="pro-001">
        <input type="text" name="product_price[]" value="$12.00">
        <input type="text" name="product_stock[]" value="10">
    </p>
    <p>
        <input type="text" name="product_name[]" value="Product #2">
        <input type="text" name="product_sku[]" value="pro-002">
        <input type="text" name="product_price[]" value="$12.00">
        <input type="text" name="product_stock[]" value="10">
    </p>
    <p><button type="submit">Add Product</button></p>
</form>

I need to process these received data, to later work with them more easily, for example adding it to the database. But I get the code this way, a structure that doesn't make things much easier for working with that data.

Array
(
    [product_name] => Array
        (
            [0] => Product #1
            [1] => Product #2
        )

    [product_sku] => Array
        (
            [0] => pro-001
            [1] => pro-002
        )

    [product_price] => Array
        (
            [0] => $12.00
            [1] => $12.00
        )

    [product_stock] => Array
        (
            [0] => 10
            [1] => 10
        )

)

I wish I could receive the code like this:

Array
(
    [0] => Array
        (
            [product_name] => Product #1
            [product_sku] => pro-001
            [product_price] => $12.00
            [product_stock] => 10
        )

    [1] => Array
        (
            [product_name] => Product #2
            [product_sku] => pro-002
            [product_price] => $12.00
            [product_stock] => 10
        )

)

I have achieved it in the following way, but I want to do it in a more optimal way.

if(isset($_POST) && !empty($_POST)) {                
    // Total products to add
    $total_products = count($_POST["product_name"]);
    // Products ordered
    $products_created = [];

    for ($i=0; $i <$total_products ; $i++) {
        $products_created[$i] = array(
            'product_name' => $_POST["product_name"][$i],
            'product_sku' => $_POST["product_sku"][$i],
            'product_price' => $_POST["product_price"][$i],
            'product_stock' => $_POST["product_stock"][$i]
        );
    }

   echo "<pre>"; print_r($_POST); 
   echo "<pre>"; print_r($products_created); 
}

Complete example code:

<?php

if(isset($_POST) && !empty($_POST)) {                
    // Total products to add
    $total_products = count($_POST["product_name"]);
    // Products ordered
    $products_created = [];

    for ($i=0; $i <$total_products ; $i++) {
        $products_created[$i] = array(
            'product_name' => $_POST["product_name"][$i],
            'product_sku' => $_POST["product_sku"][$i],
            'product_price' => $_POST["product_price"][$i],
            'product_stock' => $_POST["product_stock"][$i]
        );
    }

   echo "<pre>"; print_r($_POST); 
   echo "<pre>"; print_r($products_created); 
}

 ?>

<form action="" method="post">
    <h1>Products</h1>       
    <p>
        <input type="text" name="product_name[]" value="Product #1">
        <input type="text" name="product_sku[]" value="pro-001">
        <input type="text" name="product_price[]" value="$12.00">
        <input type="text" name="product_stock[]" value="10">
    </p>
    <p>
        <input type="text" name="product_name[]" value="Product #2">
        <input type="text" name="product_sku[]" value="pro-002">
        <input type="text" name="product_price[]" value="$12.00">
        <input type="text" name="product_stock[]" value="10">
    </p>
    <p><button type="submit">Add Product</button></p>
</form>
4
  • Do you have access to change the HTML? Commented Jun 13, 2020 at 3:35
  • If I have access to change the HTML, but the user who added the fields could not do it. Commented Jun 13, 2020 at 3:37
  • Not sure if it will work, but you can try this: name="product[].name" and name="product[].sku" etc. Usually I add javascript to dynamically build the array for the back-end rather than relying on the automatic array feature. Commented Jun 14, 2020 at 16:16
  • It's a very good idea Commented Jun 15, 2020 at 22:56

1 Answer 1

1

Solution in PHP

You could use a transpose function, that will turn your $_POST data into the desired data structure:

function transpose($array) {
    foreach ($array as $key => $values) {
        foreach ($values as $i => $value) $result[$i][$key] = $value;
    }
    return $result;
}

call as:

$products_created = transpose($_POST);

Solution in HTML

You could get the desired structure directly in PHP $_POST, if you change your HTML to this:

<form action="" method="post">
    <h1>Products</h1>       
    <p>
        <input type="text" name="product[0][name]" value="Product #1">
        <input type="text" name="product[0][sku]" value="pro-001">
        <input type="text" name="product[0][price]" value="$12.00">
        <input type="text" name="product[0][stock]" value="10">
    </p>
    <p>
        <input type="text" name="product[1][name]" value="Product #2">
        <input type="text" name="product[1][sku]" value="pro-002">
        <input type="text" name="product[1][price]" value="$12.00">
        <input type="text" name="product[1][stock]" value="10">
    </p>
    <p><button type="submit">Add Product</button></p>
</form>

You would need some logic to inject that sequential number in the first bracket pairs in the name attributes. Assuming that this HTML is produced by PHP, it would not be hard to do that. If your HTML is dynamic on the client side, where rows can be added without interaction with the server, then you'll need to do this (also) in JavaScript.

I suppose you'll have no problem in setting that up.

Sign up to request clarification or add additional context in comments.

1 Comment

Very good solution, if I can do this with Javascript, and I will, thank you very much for the idea, God bless you

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.