Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing CSV to get specific columns
    text
    copied!<p>I have a CSV file with several headers.</p> <p>I only need about 5 of these columns.</p> <p>I'm trying to get these into a more manageable format (variables?) so I can then do a check on their values.</p> <p>I have the following code:</p> <pre><code> $headers = array('NAME', 'EMAIL'); $picked = array(); $theData = array(); $isFirstRow = true; if (($handle = fopen($uploadedFile, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $numCols = count($data); $row = array(); if($isFirstRow) { for($c=0; $c&lt;$numCols; $c++) { if(!in_array($data[$c], $headers)) { continue; } else { $picked[] = $c; $isFirstRow = false; } } } else { for($c=0; $c &lt; $numCols; $c++) { if(in_array($c, $picked)) { $row[] = $data[$c]; $theData[] = $row; } } } } fclose($handle); } var_dump($theData); </code></pre> <p>This outputs the following:</p> <pre><code>array (size=xxxxxx) 0 =&gt; array (size=1) 0 =&gt; string 'John Doe' (length=8) 1 =&gt; array (size=2) 0 =&gt; string 'John Doe' (length=8) 1 =&gt; string 'johndoe@test.com' (length=16) 2 =&gt; array (size=1) 0 =&gt; string 'Jane Doe' (length=8) 3 =&gt; array (size=2) 0 =&gt; string 'Jane Doe' (length=8) 1 =&gt; string 'janedoe@test.com' (length=16) </code></pre> <p>Obviously this isn't the expected output</p> <p>I'd like something more like:</p> <pre><code>array (size=xxxx) 0 =&gt; array (size=1) 0 =&gt; string 'John Doe' (length=8) 1 =&gt; string 'johndoe@test.com' (length=16) 1 =&gt; array (size=2) 0 =&gt; string 'Jane Doe' (length=8) 1 =&gt; string 'janedoe@test.com' (length=16) </code></pre> <p>I'not sure why its adding the extra arrays.</p> <p>Anyone have an idea?</p> <p>Thanks</p> <p>EDIT</p> <p>My CSV looks like this;</p> <pre><code>NAME,EMAIL John Doe,johndoe@test.com Jane Doe,janedoe@test.com </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload