html多维数组到php多维数组 - 数组覆盖

时间:2021-07-27 21:30:48

<div id="pkg1-items">
  <div class="col-md-2 ">
    <label>Item 1 :</label>

  </div>
  <div class="col-md-2 ">
    <div class="input-control select " data-role="input">
      <input name="item[0][name]" type="text " placeholder="Enter Item " required>
    </div>
  </div>
  <div class="col-md-2 ">
    <label>Option 1 :</label>

  </div>
  <div class="col-md-2 ">
    <div class="input-control select " data-role="input">
      <input name="item[0][option1]" type="text " placeholder="Enter Option To Item" required>
    </div>
  </div>
  <div class="col-md-2 ">
    <label>Option 2 :</label>

  </div>
  <div class="col-md-2 ">
    <div class="input-control select " data-role="input">
      <input name="item[0][option2]" type="text " placeholder="Enter Option To Item  " required>

    </div>
  </div>

script which dynamically adds input

动态添加输入的脚本

$(document).on('click', 'label[id^="pkg"]', function() {
            var pkg = this.id;
            var clickeditem  = '#'+this.id +'-items';
            i++;
            $('<div class="clearfix"></div><div class="col-md-2 "> <label>Item  : </label></div><div class="col-md-2 "> <div class="input-control select " data-role="input"> <input name="item['+i+']['+'name'+']" type="text " placeholder="Enter Item " required> </div></div><div class="col-md-2 "> <label>Option 1 : </label></div><div class="col-md-2 "> <div class="input-control select " data-role="input"> <input name="item['+i+']['+'option1'+']" type="text " placeholder="Enter Option To Item" required> </div></div><div class="col-md-2 "> <label>Option 2 : </label></div><div class="col-md-2 "> <div class="input-control select " data-role="input"> <input name="item['+i+']['+'option2'+']" type="text " placeholder="Enter Option To Item " required> </div></div>').appendTo(clickeditem); 


            return false;
    });

send.php

$i=0;
        //$string = "";
        $items = array( array() ); 
        $item = $_POST['item'];
        $icnt = count($items); 
        $pkg = $_POST['Packages'];
        $nop = count($_POST['Packages']);

        for ( $i = 0 ; $i< $icnt ; $i ++ ) {
            $items[$i][0] = $item[$i]['name'];
            $items[$i][1] = $item[$i]['option1'];
            $items[$i][2] = $item[$i]['option2'];



        } 
    echo '<br/><hr/>';
            print_r($items);
            echo '<br/><hr/>';
    }

The above gives output - Array ( [0] => Array ( [0] => item1[1] => option1[2] => option2) )

上面给出了输出 - 数组([0] =>数组([0] =>项目1 [1] =>选项1 [2] =>选项2))

given that the inputs were - item1 , option1 , option2 . Although three or more inputs were added but the array gets overwritten and outputs the last value only . how can I use a dynamic multidimensional array in php . If I define the array with specific row and cols the above will work fine .

假设输入是 - item1,option1,option2。虽然添加了三个或更多输入但是数组被覆盖并仅输出最后一个值。我怎样才能在php中使用动态多维数组。如果我使用特定的行和列定义数组,上面的工作正常。

1 个解决方案

#1


0  

$items[$i][] = $item[$i]['name'];
        $items[$i][] = $item[$i]['option1'];
        $items[$i][] = $item[$i]['option2'];

removed the index from the $items[$i][0] to $items[$i][]

从$ items [$ i] [0]删除索引到$ items [$ i] []

Problem solved.

#1


0  

$items[$i][] = $item[$i]['name'];
        $items[$i][] = $item[$i]['option1'];
        $items[$i][] = $item[$i]['option2'];

removed the index from the $items[$i][0] to $items[$i][]

从$ items [$ i] [0]删除索引到$ items [$ i] []

Problem solved.