0

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.

3
  • 2
    $$servername[] might be the problem. It might be interpreting it at ${$servername[]} where you want it to interpret as ${$servername}[]. Try putting those curly-brackets in there and see if that helps. Commented May 6, 2013 at 15:39
  • I ran Joe's suggestion and it appears to fix your error. Commented May 6, 2013 at 15:45
  • It fixed it - Thanks!!! I have a similar problem further down the code that is not exactly the same but I'm playing with it now to see if I can figure it out also. Commented May 6, 2013 at 15:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.