Jump to content

Fatal error: Cannot use string offset as an array


lordshoa

Recommended Posts

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 ?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.