lordshoa Posted July 16, 2012 Share Posted July 16, 2012 Trying to understand this error a little more. Fatal error: Cannot use string offset as an array I have a csv fie that is put in to an array I then count lines the array and my var comes out as $result = array titles [$i] = the count ['proid'] = string id $result[$i]['proid'] Am I using the output wrong as I am using it as a $var should I be putting it as $key as $value instead ? So I use it as if ($result[$i]['proid'] == 2222){ do something} Is this wrong ? I was updating it on my dev machine and was getting the error in php 5.3 but not in 5.2 or 5.4 bug or change ? Quote Link to comment https://forums.phpfreaks.com/topic/265730-fatal-error-cannot-use-string-offset-as-an-array/ Share on other sites More sharing options...
requinix Posted July 16, 2012 Share Posted July 16, 2012 $result is not an array. What is the code that generates it? Quote Link to comment https://forums.phpfreaks.com/topic/265730-fatal-error-cannot-use-string-offset-as-an-array/#findComment-1361796 Share on other sites More sharing options...
lordshoa Posted July 16, 2012 Author Share Posted July 16, 2012 I use a function csvToArr So result has to be an array which is the title of the cvs colum The problem is it works correctly with php5.2 and php5.4 but in 5.3 it was giving out the error (Fatal error: Cannot use string offset as an array) I am wondering if it will suddenly break, if the server gets updated to a php version that stops it working how it is now. How could I make it different but with the same code as this runs as a cron. function csvToArr($filepath) { $headings = array(); // Array of headings $filedata = array(); // Data Array // Get file handle $handle = fopen($filepath, "r")or die('cannot open end.txt to get array'); // Get field headings $fileline = fgetcsv($handle, filesize($filepath), ";"); foreach( $fileline as $field ) { $headings[] = $field; } // Get data $line = 0; if ($line == 0 ){ while (($fileline = fgetcsv($handle, filesize($filepath), "\t")) !== FALSE) { foreach( $headings as $k => $v ) { // Store data against field headings $filedata[$line][$v] = $fileline[$k]; } $line++; } fclose($handle); } return $filedata; } $result = csvToArr($filename); Quote Link to comment https://forums.phpfreaks.com/topic/265730-fatal-error-cannot-use-string-offset-as-an-array/#findComment-1361845 Share on other sites More sharing options...
Barand Posted July 16, 2012 Share Posted July 16, 2012 has the format of your input text file been changed? Quote Link to comment https://forums.phpfreaks.com/topic/265730-fatal-error-cannot-use-string-offset-as-an-array/#findComment-1361858 Share on other sites More sharing options...
lordshoa Posted July 16, 2012 Author Share Posted July 16, 2012 Like I said it worked in php5.2 and then again in php5.4, so the file / code has not changed as I tried it on all 3 as I upgraded from 5.2 to 5.3 which broke the code so I tried on to 5.4 which it worked again. I just want to figure out why it came up as an error and what could I do about it if this happens on the live server as I cannot upgrade or downgrade the php to resolve the issue. Would this solve it ? or should it be if(is_array($result)){ do something or error } $result = array(csvToArr($filename)); Quote Link to comment https://forums.phpfreaks.com/topic/265730-fatal-error-cannot-use-string-offset-as-an-array/#findComment-1361859 Share on other sites More sharing options...
Barand Posted July 16, 2012 Share Posted July 16, 2012 what does this give when it fails? echo '<pre>', print_r($result, 1), '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/265730-fatal-error-cannot-use-string-offset-as-an-array/#findComment-1361872 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.