I need to extract data from a CSV file and insert it into a MySQL database.
(see sample file below)
So far I have an array with the entire file (reading file with PHP):
Array ([0] => 01 [1] => 12345678X [2] => Title [3] => 120.00 ...etc.
How can I modify the array to create groups of four for each item? Each item is its own array?
For example:
array( array (01,ISBN,Name,Price),
array (01,ISBN,Name,Price)
);
Additionally, how can I access those values individually and insert into MySQL?
Ex:
$price = 1.25; <--in array
This is what I have so far:
$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++;
}
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.
Sample file:
ID 01
Title This is the title
Price 120.00
ISBN xxxxxx
^
ID 02
Title This is the title
Price 20.00
ISBN xxxxxx
^