Hi All,

I'm building a script to extract data from a csv file and import it into a database, I made a script previously which worked fine, I've adapted it (added a couple of columns) to import different data but I keep getting errors

Warning: Illegal string offset 'Purchase Date' in C:\xampp\htdocs\assets\csv\csvimport.php on line 116
Warning: Illegal string offset 'Comments' in C:\xampp\htdocs\assets\csv\csvimport.php on line 117
Warning: Illegal string offset 'Location' in C:\xampp\htdocs\assets\csv\csvimport.php on line 118
Warning: Illegal string offset 'User' in C:\xampp\htdocs\assets\csv\csvimport.php on line 119
Warning: Illegal string offset 'OldLocation' in C:\xampp\htdocs\assets\csv\csvimport.php on line 120

line 116

$purchase = $mac['Purchase Date'];

I've echo'd each key before and that is working fine but when I try to add each key to a variable ready for to import in to the database I get the warnings.

my code for the relevant part is

            echo "<br /><br /><strong>Name:</strong> ".$mac['Name']."<br />";
            echo "<strong>model:</strong> ".$mac['Model']."<br />";
            echo "<strong>serial:</strong> ".$mac['Serial']."<br />";
            echo "<strong>mac:</strong> ".$mac['Mac']."<br />";
            echo "<strong>purchase:</strong> ".$mac['Purchase Date']."<br />";
            echo "<strong>comments:</strong> ".$mac['Comments']."<br />";
            echo "<strong>location:</strong> ".$mac['Location']."<br />";
            echo "<strong>user:</strong> ".$mac['User']."<br />";
            echo "<strong>oldlocation:</strong> ".$mac['OldLocation']."<br />";

      $name = makeuserfriendly($mac['Name']);
        $model = makeuserfriendly($mac['Model']);
        $serial = makeuserfriendly($mac['Serial']);
        $mac = makeuserfriendly($mac['Mac']);
       $purchase = $mac['Purchase Date'];
        $comments = makeuserfriendly($mac['Comments']);
        $location = makeuserfriendly($mac['Location']);
        $user = makeuserfriendly($mac['User']);
        $oldlocation = makeuserfriendly($mac['OldLocation']);

When I print_r $mac I get this

Array ( [0] => test [1] => 20 [2] => w89364aa66h [3] => test [4] => 10/09/2009 [5] => Media Studies [6] => test [7] => test [8] => test [Name] => test [Model] => 20 [Serial] => w89364aa66h [Mac] => test [Purchase Date] => 10/09/2009 [Comments] => Media Studies [Location] => test [User] => test [OldLocation] => test ) 

Does anyone have any ideas whey it stops working half way through assigning each key to a variable. I have tried using key values instead of names but I still get the same warnings.

Thanks
Kevin

    The problem is this line: $mac = makeuserfriendly($mac['Mac']); You reassign $mac from a variable representing an array, to a variable representing a string. Then on the next line, you try to access a string offset accessor of the string, which is impossible (strings only support numeric offsets).

      Notice that you wrote:

      $mac = makeuserfriendly($mac['Mac']);

      This overwrites $mac, so instead of the original array, you end up with the return value of makeuserfriendly($mac['Mac']).

      By the way, what database are you using and what is the implementation of makeuserfriendly?

        Thanks guys, I knew it was something simple I've been staring at it too long to notice.

        I'm using MS SQL 2012 and the makeuserfriendly is

        function makeuserfriendly($data)
        {
        	if ( !isset($data) or empty($data) ) return '';
                if ( is_numeric($data) ) return $data;
        
        $non_displayables = array(
        	'/%0[0-8bcef]/',	// url encoded 00-08, 11, 12, 14, 15
        	'/%1[0-9a-f]/',		// url encoded 16-31
        	'/[\x00-\x08]/',	// 00-08
        	'/\x0b/',		// 11
        	'/\x0c/',		// 12
        	'/[\x0e-\x1f]/'		// 14-31
        );
        foreach ( $non_displayables as $regex )
        	$data = preg_replace( $regex, '', $data );
        $data = str_replace("'", "''", $data );
        return $data;
        }
          Write a Reply...