Undefined offset error

Hello there,

I have searched the forum for some solutions bordering on the above named error but it seems every one has got their own perculiar issue same as me hence my request for some suggestion in my own case.

I have the following query in my scripts which is working but I have issues dealing with the undefined offset error notices which am not too comfortable with though the output it ok.

$query = "INSERT INTO rmc_raw_data(device_id,nmea,rmc_time, signal, latitude, north, longitude, east, speed, track_angle, rmc_date, magnetic_variation, west,check_sum,handovers,data_source,test_case_id) values";
		 while (($data = fgetcsv($handle)) !== FALSE) 
			$myDay=substr($data[10], 0,2);
			$myMonth=substr($data[10], 2,2);
			$myYear=substr($data[10], 4,2);			
			$rmc_date = convertToNiceDate($myDay, $myMonth, 2000+$myYear);
            $longitude = convertToDegrees($data[6]);
            $latitude  = convertToDegrees($data[4]);
			//$query .="('$data[0]','$data[1]','$data[2]','$latitude','$data[4]','$longitude','$data[6]','$data[7]','$data[8]','$rmc_date'),";
            $query .="('$data[0]','$data[1]','$data[2]','$data[3]','$latitude','$data[5]','$longitude','$data[7]','$data[8]','$data[9]','$rmc_date','$data[11]','$data[12]','$data[13]', '$data[14]','$data[15]', '$data[16]'),";
		$query = substr($query, 0,-1);
		//echo $query;
		mysql_query($query) or die(mysql_error());

The error is this:


 ! ) Notice: Undefined offset: 14 in C:\\wamp\\www\\setapro\\mobile_importmanager.php on line 68
( ! ) Notice: Undefined offset: 15 in C:\\wamp\\www\\setapro\\mobile_importmanager.php on line 68
( ! ) Notice: Undefined offset: 16 in C:\\wamp\\www\\setapro\\mobile_importmanager.php on line 68

… and continous like that till the end of script.

I do know that the problem is around the row data values of







I think my PHP skills are still wanting in resolving this matter as am still learning. So could someone please tell me why is this? Many thanks.


It means there are no values (or rather data) at those array indices. :slight_smile:

$array = array(

echo $array[0]; #foo
echo $array[1]; #bar
echo $array[2]; #undefined offset

An offset is undefined if it doesn’t exist in the array. The notice is thrown the first time you reference the element, then PHP initializes the element and sets it to NULL.

To avoid the notice you must initialize the offset. You can test if an offset exists either through array_key_exists or using isset. Example

if (!isset($data[14])) {
  $data[14] = '';

if (!array_key_exists(15, $data)) {
  $data[15] = '';

nevermind. Misread the sample.

Thanks for your useful reply. The fact is that those arrays correpond to the the data fields in the query where insertion is done. How do I initialise in another way.

I would perform the test to see the output. thanks.

Do I take it that, it should have been another solution when you say nervermind?

I think your solution is accurate Michael - checking for the existance of the array element before using it would solve the issue.

So if the value exists - use it otherwise set a default value.

Just did the test. The field is empty. Those field are suppose to be correponding to the columns I have in the insert part of the query. I don’t get this.

Worked like magic! Many thanks for you help.

I misread the post immediately above mine and posted something erroneous, noted the mistake and blanked out the post. I would have simply deleted the post if allowed.

Off Topic:

It is allowed. Just click “edit” and then click “delete”. Within half an hour after posting before the edit button disappears that is.