PhP关联数组可更有效

时间:2018-10-22 12:12:16

标签: php arrays maintainability

我想问一下我是否有表格中的数组。但是我需要管理数组。

<div class="item form-group">
<h4>Program List</h4>
</div>
<div class="new_program">
  <div class="item form-group">  
  <label class="control-label col-md-3 col-sm-3 col-xs-12"></label>                      
  <div class="col-md-6 col-sm-4 col-xs-8">
  <input  type="text" name="desciplines[0]['name']" placeholder="Program Name" class="form-control col-md-7 col-xs-12">                          
 </div>
</div>
 <div class="item form-group">  
 <label class="control-label col-md-3 col-sm-3 col-xs-12"></label>  
 <div class="col-md-6 col-sm-6 col-xs-12">
 <textarea name="desciplines[0]['program_desc']" class="form-control col-md-7 col-xs-12"></textarea>
 </div>
 </div>
 <div class="item form-group">
 <label class="control-label col-md-3 col-sm-3 col-xs-12"></label>  
 <div class="col-md-2 col-sm-2 col-xs-12">
 <input  type="text" name="desciplines[0]['fees']" placeholder="fees" class="form-control col-md-7 col-xs-12">
 </div>
 </div>
 </div>  
 <div class="item form-group">  
 <label class="control-label col-md-3 col-sm-3 col-xs-12"></label>  
  <div class="col-md-6 col-sm-6 col-xs-12">
 <i class="fa fa-plus add_descipline"></i>
 </div>
 </div>



jQuery是

$(document).on('click','.add_descipline',function(e){   
            var html = $('.new_program').wrap('<p/>').parent().html();
            $( html ).insertAfter( ".new_program" );
});


即使现在,我现在还是用临时检查输出更改了带有inspect元素的增量索引,然后将更改jQuery。

enter image description here



对不起,如果您不明白。请让我知道。我会更真诚地解释。


问候

2 个答案:

答案 0 :(得分:1)

如Riggs在评论中所说,最好以当前状态处理阵列。 但是,如果您想坚持自己的计划,则可以执行以下操作:

$new_desciplines = [];
foreach ($_POST as $key => $value) {
    if(in_array($key, ["desciplines", "programs_descriptions", "fees"])){
        foreach ($value as $index => $val) {
            $new_desciplines[$index][$key] = $val;
        }
    }
}

然后使用$new_desciplines变量

print_r($new_desciplines);

答案 1 :(得分:0)

<?php
echo "<pre>";
print_r($_POST);
?>

  <form class="form-horizontal"  enctype="multipart/form-data" action="#" method="post">
      <input  type="text" name="desciplines[0]['name']" placeholder="Name">  
  <input  type="text" name="desciplines[0]['desc']" placeholder="desc">  
  <input  type="text" name="desciplines[0]['fees']" placeholder="fees">    

  <input  type="text" name="desciplines[1]['name']" placeholder="Name">  
  <input  type="text" name="desciplines[1]['desc']" placeholder="desc">  
  <input  type="text" name="desciplines[1]['feed']" placeholder="fees">    

      <input type="submit" value="Submit">
      </form>

out put

Array
(
    [desciplines] => Array
        (
            [0] => Array
                (
                    ['name'] => fads
                    ['desc'] => fads
                    ['fees'] => fads
                )

            [1] => Array
                (
                    ['name'] => fadsf
                    ['desc'] => fdasfdas
                    ['feed'] => fdasf
                )

        )

)