I am uploading a csv file and then parsing it using str_getcsv. All works great except that I need a way to cycle through them. Ideally, it\'d be great to have the array com
Not the pretties thing on this world but I think it works.
//make the arrays needed
$csvtmp = array();
$csvFinal = array();
$csvfile = file_get_contents('test.csv');
$csv = str_getcsv($csvfile,"\n"); //make an array element for every record (new line)
//Loop through the result
foreach ($csv as $key => $item)
{
array_push($csvtmp, str_getcsv($item,";")); //push each record to the csv temp array
//here's the messy part. This set the key of the csvFinal array to the first field in the current record and for the value it gives it the array we got from the previous str_getcsv.
$csvFinal[$csvtmp[$key][0]] = $csvtmp[$key];
//Here we just unset the id row in the final array
unset($csvFinal[$csvtmp[$key][0]][0]);
}
echo "";
print_r($csvFinal);
echo "
";
result of the code:
Array
(
[22] => Array
(
[1] => name1
[2] => address1
[3] => town1
[4] => state1
[5] => zip1
[6] => phone1
[7] => website1
[8] => other1
[9] => other1
)
[23] => Array
(
[1] => name2
[2] => address2
[3] => town2
[4] => state2
[5] => zip2
[6] => phone2
[7] => website2
[8] => other1
[9] => other1
)
[24] => Array
(
[1] => name3
[2] => address3
[3] => town3
[4] => state3
[5] => zip3
[6] => phone3
[7] => website3
[8] => other1
[9] => other1
)
}
Hope this helps