Is this iteration key by key the proper way of initializing custom named keys for my str_getcsv array values ? Also is the unset() expensive or even necessary in this loop ?
<?php
$lines = file('csv/random.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
$temp = str_getcsv($value,'|');
$csv[$key]['code'] = $temp[0];
$csv[$key]['name'] = $temp[1];
$csv[$key]['price'] = $temp[2];
$csv[$key]['avail'] = $temp[3];
unset ($temp);
}
?>
EDIT:
Following the advice in comments the code looks neater and runs significantly faster now
$keys = Array('code','name','price','avail');
$file = fopen('csv/random.csv',"r");
while(! feof($file))
{
$temp[] = array_combine($keys,fgetcsv($file,'1000','|'));
}
fclose($file);
The answer marked below is I believe a more professional implementation of this for those who have need of such.
Thanks guys.