title: How can ignore the second and third array when there is no data?
body: <p>I have below code on my page and I am displaying dynamically field.</p>
  <pre><code>&lt;input type=&quot;file&quot; name=&quot;pic[1]&quot;   class=&quot;work_pic&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;title[1]&quot; class=&quot;form-control&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;desc[1]&quot;  class=&quot;form-control&quot;&gt;
  
  
  &lt;input type=&quot;file&quot; name=&quot;pic[2]&quot;   class=&quot;work_pic&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;title[2]&quot; class=&quot;form-control&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;desc[2]&quot;  class=&quot;form-control&quot;&gt;
  
  
  &lt;input type=&quot;file&quot; name=&quot;pic[3]&quot;   class=&quot;work_pic&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;title[3]&quot; class=&quot;form-control&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;desc[3]&quot;  class=&quot;form-control&quot;&gt;
  </code></pre>
  <p>Below is my logic code</p>
  <pre><code>$title=$_POST['title'];
  $desc=$_POST['desc'];
  foreach($title as $key =&gt;$value){ 
      $ogimage=$_FILES['pic']['name'][$key];
      //print_r($ogimage);
      $work_pic =&quot; &quot;;
      $work_dir = &quot;assets/images/uploads/pic/&quot;;
      $filename1  = basename($ogimage);
      $extension1 = pathinfo($filename1, PATHINFO_EXTENSION);
      $newrand        = mt_rand();// random number
      $work_pic       = $newrand.'.'.$extension1;
      $target_file = $work_dir . $work_pic;
      $uploadOk = 1;
  
      if ($uploadOk == 0) {
          $errorMsg[]=&quot;Sorry, your file was not uploaded dynamic.&quot;;
          $code=&quot;5&quot;;
      } else {
          if (move_uploaded_file($_FILES[&quot;pic&quot;][&quot;tmp_name&quot;][$key], $target_file)) {
              $workimagename= $work_pic;
          } else {
              echo &quot;Sorry, there was an error uploading your file.&quot;;
          }
      }
  
      $insert_array = array(
                'title' =&gt; $title[$key],
                'desc' =&gt; $desc[$key],
                'pic' =&gt; $workimagename,
                'user_id' =&gt; $lastUserid,
            );
        
      $sqlwork=&quot;INSERT INTO `tbl_abcwork`
                      (`title`, `desc`, `pic`, `user_id`) 
              VALUES (:title,:desc,:pic,:user_id)&quot;;
      $stmt= $pdo-&gt;prepare($sqlwork);
      $stmt-&gt;execute($insert_array);  
  </code></pre>
  <p>My logic is working perfectly if I filled all three with data. I am getting the output</p>
  <pre><code>Array ( 
      [title] =&gt; testing 
      [desc] =&gt; testing 
      [pic] =&gt; 1756775265.png 
      [user_id] =&gt; 6
     ) 
    Array (
     [title] =&gt; testing 
     [desc] =&gt; testing 
     [pic] =&gt; 869702765.png
      [user_id] =&gt; 6 
    )
    Array (
       [title] =&gt; testing 
       [desc] =&gt; testing 
       [pic] =&gt; 1145700947.png 
       [user_id] =&gt; 6 
     ) 
  </code></pre>
  <p>If I filled data only in the first fieldset then I am getting output.</p>
  <pre><code>Array ( 
    [work_title] =&gt; alndlasd 
    [work_year] =&gt; lknalksndlaksd 
    [work_pic] =&gt; 1332366488.png 
    [user_id] =&gt; 7 
  ) Sorry, there was an error uploading your file.
  
  Array ( 
    [work_title] =&gt; 
    [work_year] =&gt; 
    [work_pic] =&gt; 1332366488.png 
    [user_id] =&gt; 7 
  ) Sorry, there was an error uploading your file.
  Array ( 
    [work_title] =&gt; 
    [work_year] =&gt; 
    [work_pic] =&gt; 1332366488.png 
    [user_id] =&gt; 7
     ) 
  </code></pre>
  <p>I am getting the data in my database.</p>
  <p><a href="https://i.stack.imgur.com/hifMq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hifMq.png" alt="enter image description here" /></a></p>
  <p>Issues</p>
  <ol>
  <li>I am getting the same image in 2 and third array</li>
  <li>How can ignore the 2 and 3 array when there is no data?</li>
  </ol>