title: PHP Read File into Array
body: <p>I need to extract data from a CSV file and insert it into a MySQL database.</p>
  
  <p>(see sample file below)</p>
  
  <p>So far I have an array with the entire file (reading file with PHP):</p>
  
  <p><code>Array ([0] =&gt; 01 [1] =&gt; 12345678X [2] =&gt; Title [3] =&gt; 120.00</code> ...etc.</p>
  
  <p>How can I modify the array to create groups of four for each item? Each item is its own array?</p>
  
  <p>For example:</p>
  
  <pre><code>array( array (01,ISBN,Name,Price),
         array (01,ISBN,Name,Price)
        );
  </code></pre>
  
  <p>Additionally, how can I access those values individually and insert into MySQL?</p>
  
  <p>Ex:
  <code>$price = 1.25;</code> &lt;--in array</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>    $fp = @fopen($_FILES['filename']['tmp_name'], "r");
  
      if ($fp) {
  
           $arr = array();
           while(!feof($fp)) {
            $this_line = fgets($fp);  
  
            $line = explode("^",$this_line);
  
            if($line[0] != "") {          
                array_push($arr,$line[0]);
                }
            $i++;
           }
  </code></pre>
  
  <p>The above code returns one big array with each column an an element. I would like arrays of four elements per array within one large array so that I can access each "row" individually.</p>
  
  <p>Sample file:</p>
  
  <pre><code>ID 01
  Title This is the title
  Price 120.00
  ISBN xxxxxx
  ^
  ID 02
  Title This is the title
  Price 20.00
  ISBN xxxxxx
  ^
  </code></pre>