G-J
G-J

Reputation: 1068

Cannot use string offset as an array (What does it mean?)

The code below is more or less a chunk of my code. The $servername and $monthlyincome variables are not actually static as shown here but I changed them so I could add less code here.

If I run this code...

$servername="Server1";

$months = array('January','February','March','April','May','June','July','August','September','October','November','December');


for ($i=0;$i<=24;$i++) {
    $new_time = mktime(0,0,0,date("m")+$i,1,date("Y"));
    $months_array[date("Y",$new_time)][date("m",$new_time)] = "x";
}

$overallincome = 0;

foreach ($months_array AS $year=>$month) {
    foreach ($month AS $mon=>$x) {
        $monthlyincome = 3;
        $overallincome += $monthlyincome;

        $$servername[$months[$mon-1]." ".$year]['monthlyincome']=$monthlyincome;
        $$servername[$months[$mon-1]." ".$year]['overallincome']=$overallincome;

    }
}

I get this error...

Cannot use string offset as an array in on line 123

Line 123 is this line... $$servername[$months[$mon-1]." ".$year]['monthlyincome']=$monthlyincome;

I can't figure out what I am doing wrong. I have checked other posts on SO with the same error but nothing made sense to me.

Upvotes: 0

Views: 93

Answers (1)

Wolfman Joe
Wolfman Joe

Reputation: 799

Putting it as an answer, then!

$$servername[] seems to be the problem. It's interpreting it as ${$servername[]} where you want it to interpret as ${$servername}[].

Try putting those curly-brackets in there and see if that helps.

Upvotes: 1

Related Questions