PhP associative array to get more effective

I would like to ask that I am having array from the form. but I need to manage the array.

<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 is

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


Even though right now, I have changed the increment index with inspect element for the temporary output for now, then I will change the jQuery.

enter image description here



Sorry, if you not understand. Just let me know. I will explain more sincerely.


Regards

2 answers

  • answered 2018-10-22 12:26 Bhale Dino

    <?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
                    )
    
            )
    
    )
    

  • answered 2018-10-22 12:41 ringunger

    It is better to process the array with its current state as Riggs says in comments. But if you want to stick with your plan, you can do this:

    $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;
            }
        }
    }
    

    Then use the $new_desciplines variable

    print_r($new_desciplines);