I have a script which imports data from csv and creates variables from the data which are imported into sql tables. It all works fine but the error log goes crazy with Undefined Offset errors.
From what I have read it is because it only finds index [0] so any others throw up the error.
I've looked at the following and can't seem to get the suggestions to work:
PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"
PHP Notice: Undefined offset error when importing from .csv and many more...
Here is the code:
$filename='XXXXX_'.$today.'.csv';
$url=$proto.$user.':'.$pass.'@'.$server.$path.$filename;
$csv_data=file_get_contents($url);
foreach(preg_split("/((\r?\n)|(\r\n?))/", $csv_data) as $line){
list($type, $id, $ref) = explode(',', $line);
The error is generated on the 'list' and 'explode' line of code.
Apologies, quite new to this, have read multiple articles on the 'why' just not found a fix, it is the only error type i have populating the errorlog. Any help would be much appreciated.
you explode must return least 3 output , if in the line exist less than 3 item , error will be show. For this case you can use:
$lineData = explode(','$line);
$type = isset($lineData[0]) ? $lineData[0] : null;
$id = isset($lineData[1]) ? $lineData[1] : null;
$ref = isset($lineData[2]) ? $lineData[2] : null;
Or use:
@list($type, $id, $ref) = explode(',', $line);