title: Write array to CSV file using PHP
body: <p>I am trying to insert data into CSV file by storing data into array first before writing to CSV. But I am facing a problem with writing a multidimentional array to CSV file.</p>
  
  <p>Here is how my input looked like :</p>
  
  <pre><code>&lt;input type="text" name="names" placeholder="Enter Name" /&gt;
  &lt;select name="gender"&gt;
      &lt;option value="Male"&gt;Male&lt;/option&gt;
      &lt;option value="Female"&gt;Female&lt;/option&gt;
  &lt;/select&gt;
  &lt;input type="text" name="age" placeholder="Enter Age" /&gt;
  &lt;input type="text" name="state" placeholder="Enter State" /&gt;
  
  &lt;label&gt;How Did You Find Us?&lt;/label&gt;
  
  &lt;input type="checkbox" name="remarks[]" value="Search Engine (eg. Google)"&gt; Search Engine (eg. Google) &amp;nbsp;&amp;nbsp;&amp;nbsp;
  &lt;input type="checkbox" name="remarks[]" value="Facebook"&gt; Facebook &amp;nbsp;&amp;nbsp;&amp;nbsp;
  &lt;input type="checkbox" name="remarks[]" value="Newspaper Ads"&gt; Newspaper Ads &amp;nbsp;&amp;nbsp;&amp;nbsp;
  &lt;input type="checkbox" name="remarks[]" value="Bus-Stop Ads"&gt; Bus-Stop Ads &amp;nbsp;&amp;nbsp;&amp;nbsp;
  </code></pre>
  
  <p>The following code is how I store input data and write in CSV file :</p>
  
  <pre><code>$names = filter_input(INPUT_POST, "names");
  $gender = filter_input(INPUT_POST, "gender");
  $age = filter_input(INPUT_POST, "age");
  $state = filter_input(INPUT_POST, "state");
  $remarks = $_POST['remarks'];
  
  $inputArr[] = array(
      $names, $gender, $age, $state, $remarks
  );
  
  $fp = fopen($_SERVER["DOCUMENT_ROOT"]."/student.csv", "a+");
  
  fputcsv($fp, $inputArr);
  fclose($fp);
  </code></pre>
  
  <p>The <code>$inputArr</code> output is as follow which is a multidimentional :</p>
  
  <pre><code>Array
  (
      [0] =&gt; Melinda
      [1] =&gt; Female
      [2] =&gt; 23
      [3] =&gt; united state
      [4] =&gt; Array
          (
              [0] =&gt; Facebook
              [1] =&gt; Newspaper Ads
              [2] =&gt; Bus-Stop Ads
          )
  
  )
  </code></pre>
  
  <p>I wanted to make the array be like below array so that data can be inserted into CSV, but I don't know how to foreach the array to get the output as follow:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Melinda
      [1] =&gt; Female
      [2] =&gt; 23
      [3] =&gt; united state
      [4] =&gt; Array
      [5] =&gt; Facebook
      [6] =&gt; Newspaper Ads
      [7] =&gt; Bus-Stop Ads
  )
  </code></pre>