I've been struggling with an odd problem for a couple days. I'm using the File_CSV_DataSource library to parse a csv file into an array ($data) and then transferring that data into a different array ($attributes) in order to load the records into a db.
When creating the $attributes array I'm getting an undefined index error on the first key in $data. Displaying the value of $data prior to this reveals that the key exists and has data, so I'm not sure why the error is being generated.

Any thoughts on what I'm doing wrong? Thanks!

Here is the class I'm working with:

class testArray extends CI_Controller
{
	function index()
	{
		$this->load->view('testArray/upload_form', array('error' => ' ' ));
	}

function upload_files()
{
	$config['upload_path'] = '/var/www/uploaded_files/';
	$config['allowed_types'] = 'csv|txt';
	$config['overwrite'] = TRUE;
	$config['remove_spaces'] = TRUE;
	$config['max_size'] = '200000';
	$config['file_name'] = 'test_data.csv';


	$this->upload->initialize($config);

	if ( ! $this->upload->do_upload())
	{
		$error = array('error' => $this->upload->display_errors());

		$this->load->view('upload_form', $error);
	}
	else
	{
		$data = array('upload_data' => $this->upload->data());

		$data['records'] = $this->import_data($data['upload_data']['full_path']);

		$this->load->view('testArray/upload_success', $data);
	}
}

function import_data($file = FALSE)
{
	$i = 0; $y = 0;

	if(!$file)
	{
		die('cannot import...the file was not passed to import_data');
	}

	$data = New File_CSV_DataSource;

	if($data->load($file))
	{
		if($data->isSymmetric())
		{
			foreach($data->connect() as $item)
			{
var_export($item);
					// map the file headers to the table headers
					$attributes = array(
							'site_id'				=> $item['Site Id'],
							'first_name' 			=> $item['First Name'],
							'last_name' 			=> $item['Last Name'],
							'address'				=> $item['Address'],
							'city'					=> $item['City'],
							'state'					=> $item['State'],
							'zip'					=> $item['Zip'],
							'qualification'			=> $item['Qualified/Unavailable'],
							'program_name'			=> $item['Program Name'],
							'confirmation_number'	=> $item['Confirmation Number'],
							'arrival_date'			=> $item['Arrival Dt'],
							'tour_id'				=> $item['Tourid'],
							'owner_flag'			=> $item['Owner Flag']
						);
var_export($attributes);
die();

			}
		}
		else
		{
			$array = $data->getAsymmetricRows(); //get the rows that don't match the header
			print_r($array);
			die('The file has rows that don\'t match the length of the header');
		}
	}
	return array('created' => $i, 'updated' => $y);
}

}

The above code creates this output:

array ( 'Site Id' => 'ANAHEIM', 'First Name' => 'Scott', 'Last Name' => 'McClung', 'Address' => '1 Main St', 'City' => 'Las Vegas', 'State' => 'NV', 'Zip' => '89148', 'Program Name' => 'Motel', 'Qualified/Unavailable' => 'UNQUALIFIED', 'Arrival Dt' => '04/24/2012', 'Tourid' => '', 'Confirmation Number' => '1234567RCNA', 'Owner Flag' => 'Y', )

A PHP Error was encountered

Severity: Notice

Message: Undefined index: Site Id

Filename: controllers/testArray.php

Line Number: 58

array ( 'site_id' => NULL, 'first_name' => 'Scott', 'last_name' => 'McClung', 'address' => '1 Main St', 'city' => 'Las Vegas', 'state' => 'NV', 'zip' => '89148', 'qualification' => 'UNQUALIFIED', 'program_name' => 'Motel', 'confirmation_number' => '1234567RCNA', 'arrival_date' => '04/24/2012', 'tour_id' => '', 'owner_flag' => 'Y', )

    Sure, thanks for taking a look at it!

    Here is the output using var_dump instead of var_export (and using the <pre> tags 🙂)

    array(13) {
    ["&#65279;Site Id"]=>
    string(7) "ANAHEIM"
    ["First Name"]=>
    string(5) "Scott"
    ["Last Name"]=>
    string(7) "McClung"
    ["Address"]=>
    string(9) "1 Main St"
    ["City"]=>
    string(9) "Las Vegas"
    ["State"]=>
    string(2) "NV"
    ["Zip"]=>
    string(5) "89148"
    ["Program Name"]=>
    string(5) "Motel"
    ["Qualified/Unavailable"]=>
    string(11) "UNQUALIFIED"
    ["Arrival Dt"]=>
    string(10) "04/24/2012"
    ["Tourid"]=>
    string(0) ""
    ["Confirmation Number"]=>
    string(11) "1234567RCNA"
    ["Owner Flag"]=>
    string(1) "Y"
    }

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined index: Site Id

    Filename: controllers/testArray.php

    Line Number: 60

    array(13) {
    ["site_id"]=>
    NULL
    ["first_name"]=>
    string(5) "Scott"
    ["last_name"]=>
    string(7) "McClung"
    ["address"]=>
    string(9) "1 Main St"
    ["city"]=>
    string(9) "Las Vegas"
    ["state"]=>
    string(2) "NV"
    ["zip"]=>
    string(5) "89148"
    ["qualification"]=>
    string(11) "UNQUALIFIED"
    ["program_name"]=>
    string(5) "Motel"
    ["confirmation_number"]=>
    string(11) "1234567RCNA"
    ["arrival_date"]=>
    string(10) "04/24/2012"
    ["tour_id"]=>
    string(0) ""
    ["owner_flag"]=>
    string(1) "Y"
    }

      Here is the output using that code in place of the var_dump lines...

      &#65279;Site Id = 0xEF 0xBB 0xBF 0x53 0x69 0x74 0x65 0x20 0x49 0x64
      A PHP Error was encountered

      Severity: Notice

      Message: Undefined index: Site Id

      Filename: controllers/testArray.php

      Line Number: 63
      site_id = 0x73 0x69 0x74 0x65 0x5F 0x69 0x64

      Here is the test code again for line number reference...

      <?php
      
      class testArray extends CI_Controller
      {
      	function index()
      	{
      		$this->load->view('testArray/upload_form', array('error' => ' ' ));
      	}
      
      function upload_files()
      {
      	$config['upload_path'] = '/var/www/uploaded_files/';
      	$config['allowed_types'] = 'csv|txt';
      	$config['overwrite'] = TRUE;
      	$config['remove_spaces'] = TRUE;
      	$config['max_size'] = '200000';
      	$config['file_name'] = 'test_data.csv';
      
      
      	$this->upload->initialize($config);
      
      	if ( ! $this->upload->do_upload())
      	{
      		$error = array('error' => $this->upload->display_errors());
      
      		$this->load->view('upload_form', $error);
      	}
      	else
      	{
      		$data = array('upload_data' => $this->upload->data());
      
      		$data['records'] = $this->import_data($data['upload_data']['full_path']);
      
      		$this->load->view('testArray/upload_success', $data);
      	}
      }
      
      function import_data($file = FALSE)
      {
      	$i = 0; $y = 0;
      
      	if(!$file)
      	{
      		die('cannot import...the file was not passed to import_data');
      	}
      
      	$data = New File_CSV_DataSource;
      
      	if($data->load($file))
      	{
      		if($data->isSymmetric())
      		{
      			foreach($data->connect() as $item)
      			{
      
      $keys = array_keys($item);
      echo "$keys[0] = ";
      for($i = 0; $i < strlen($keys[0]); $i++)
          printf('0x%X ', ord($keys[0][$i])); 
      
      				// map the file headers to the table headers
      				$attributes = array(
      						'site_id'				=> $item['Site Id'],
      						'first_name' 			=> $item['First Name'],
      						'last_name' 			=> $item['Last Name'],
      						'address'				=> $item['Address'],
      						'city'					=> $item['City'],
      						'state'					=> $item['State'],
      						'zip'					=> $item['Zip'],
      						'qualification'			=> $item['Qualified/Unavailable'],
      						'program_name'			=> $item['Program Name'],
      						'confirmation_number'	=> $item['Confirmation Number'],
      						'arrival_date'			=> $item['Arrival Dt'],
      						'tour_id'				=> $item['Tourid'],
      						'owner_flag'			=> $item['Owner Flag']
      					);
      $keys = array_keys($attributes);
      echo "$keys[0] = ";
      for($i = 0; $i < strlen($keys[0]); $i++)
          printf('0x%X ', ord($keys[0][$i])); 						
      
      die();
      
      			}
      		}
      		else
      		{
      			$array = $data->getAsymmetricRows(); //get the rows that don't match the header
      			print_r($array);
      			die('The file has rows that don\'t match the length of the header');
      		}
      	}
      	return array('created' => $i, 'updated' => $y);
      }
      
      }
        scottmcclung wrote:
        &#65279;Site Id = 0xEF 0xBB 0xBF 0x53 0x69 0x74 0x65 0x20 0x49 0x64

        In other words, the key "Site Id" in the $item array is not "Site Id", but rather "Site Id"; the CSV file you're reading from has a UTF-8 encoded Byte Order Mark at the start.

        You'll either need to trim that off or, preferably, not have it in the CSV file in the first place. Unless the library has some setting for the purpose, of course.

          Thanks for the help! I would've never found that.
          Have a great night!

          Scott

            Write a Reply...