@tokyodead

Как вывести информацию с формы в многомерный массив?

Подскажите, у меня есть форма с добавлением товаров, есть кнопка "Add Item" которая создает новое поле, выводится массив вот такой

Array
(
    [customerName] => 
    [product] => Array
        (
            [0] => Test
            [1] => Example
        )

    [productPrice] => Array
        (
            [0] => 2500
            [1] => 2500
        )

    [competitor1] => Array
        (
            [0] => Arthur
            [1] => Arthur
        )

    [product1] => Array
        (
            [0] => Ailex 1000мл
            [1] => Fairy
        )

    [competitorPrice1] => Array
        (
            [0] => 2500
            [1] => 1500
        )

    [competitor2] => Array
        (
            [0] => Test2
            [1] => Example2
        )

    [product2] => Array
        (
            [0] => Uilex 500мл
            [1] => Kairy
        )

    [competitorPrice2] => Array
        (
            [0] => 4000
            [1] => 1600
        )

    [competitor3] => Array
        (
            [0] => Test3
            [1] => Example3
        )

    [product3] => Array
        (
            [0] => Koilex 600мл
            [1] => Mairy
        )

    [competitorPrice3] => Array
        (
            [0] => 1000
            [1] => 900
        )

    [minPrice] => Array
        (
            [0] => 
            [1] => 
        )

    [submit] => Отправить запрос
)


Мне нужно чтобы каждый товар выводился в отдельном массиве

Array
(
    [customerName] => 
    [Items] => Array(
      [0][product] => Array
          (
              [0] => Test
          )

      [productPrice] => Array
          (
              [0] => 2500
          )

      [competitor1] => Array
          (
              [0] => Arthur
          )

      [product1] => Array
          (
              [0] => Ailex 1000мл
          )

      [competitorPrice1] => Array
          (
              [0] => 2500
          )

      [competitor2] => Array
          (
              [0] => Test2
          )

      [product2] => Array
          (
              [0] => Uilex 500мл
          )

      [competitorPrice2] => Array
          (
              [0] => 4000
          )

      [competitor3] => Array
          (
              [0] => Test3
          )

      [product3] => Array
          (
              [0] => Koilex 600мл
          )
      [1][product] => Array
          (
              [0] => Test2
          )

      [productPrice] => Array
          (
              [0] => 2500
          )

      [competitor1] => Array
          (
              [0] => Arthur
          )

      [product1] => Array
          (
              [0] => Ailex 1000мл
          )

      [competitorPrice1] => Array
          (
              [0] => 2500
          )

      [competitor2] => Array
          (
              [0] => Test3
          )

      [product2] => Array
          (
              [0] => Uilex 500мл
          )

      [competitorPrice2] => Array
          (
              [0] => 4000
          )

      [competitor3] => Array
          (
              [0] => Test4
          )

      [product3] => Array
          (
              [0] => Koilex 600мл
          )

      [competitorPrice3] => Array
          (
              [0] => 1000
          )
      )
    

    [minPrice] => Array
        (
            [0] => 
            [1] => 
        )

    [submit] => Отправить запрос
)

Вот мой код

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form class="" action="index.php" method="post">
      <p>
        <input name="customerName" placeholder="Enter customer name">
      </p>
      <table>
        <tr>
          <th>#</th>
          <th>Товар</th>
          <th>Цена</th>
          <th>Конкурент №1</th>
          <th>Товар конкурента №1</th>
          <th>Цена конкурента №1</th>
          <th>Конкурент №2</th>
          <th>Товар конкурента №2</th>
          <th>Цена конкурента №2</th>
          <th>Конкурент №3</th>
          <th>Товар конкурента №3</th>
          <th>Цена конкурента №3</th>
          <th>Минимальная цена</th>
        </tr>
        <tbody id="tbody"></tbody>
      </table>

      <p>
        <button type="button" onclick="addItem();">
          Add Item
        </button>
      </p>

      <p>
        <input type="submit" name="submit">
      </p>
    </form>

    <script>
      var items = 0;
      function addItem() {
        items++;
        var html = "<tr>";
          html += "<td>" + items + "</td>";
          html += "<td><input name='product[]'></td>";
          html += "<td><input name='productPrice[]'></td>";
          html += "<td><input name='competitor1[]'></td>";
          html += "<td><input name='product1[]'></td>";
          html += "<td><input name='competitorPrice1[]'></td>";
          html += "<td><input name='competitor2[]'></td>";
          html += "<td><input name='product2[]'></td>";
          html += "<td><input name='competitorPrice2[]'></td>";
          html += "<td><input name='competitor3[]'></td>";
          html += "<td><input name='product3[]'></td>";
          html += "<td><input name='competitorPrice3[]'></td>";
          html += "<td><input name='minPrice[]'></td>";
        html += "<tr>";

        document.getElementById('tbody').insertRow().innerHTML = html;
      }
    </script>
    <script>

    </script>
  </body>
</html>

<?php

  if(isset($_POST['submit'])) {
    $data = $_REQUEST;
    echo '<pre>';
    print_r($data);
    echo '<pre>';
  }

?>
  • Вопрос задан
  • 61 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы