<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>
&#13;
动态添加输入的脚本
$(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/>';
}
以上给出了输出 - 数组([0] =&gt;数组([0] =&gt; item1 [1] =&gt;选项1 [2] =&gt;选项2))
假设输入是 - item1,option1,option2。虽然添加了三个或更多输入但是数组被覆盖并仅输出最后一个值。我怎样才能在php中使用动态多维数组。如果我使用特定的行和列定义数组,上面的工作正常。
答案 0 :(得分:0)
$items[$i][] = $item[$i]['name'];
$items[$i][] = $item[$i]['option1'];
$items[$i][] = $item[$i]['option2'];
将$ items [$ i] [0]中的索引移至$ items [$ i] []
问题解决了。