Cannot use string offset as an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samatair
    New Member
    • Nov 2007
    • 61

    Cannot use string offset as an array

    I get an error message like this.

    Fatal error: Cannot use string offset as an array in D:\www\site\inc ludes\change_pr eference.inc.ph p on line 310

    [PHP]$cityName["$i"]["$j"] = $row2['city'];[/PHP]
    The above code is causing this error. Please let me know what is wrong with this code. How can I rectify this error.

    Here $i, $j consists of integer value. which are used to perform a for loop specific number of times. while $row2 brings data from a database.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    try $cityName's keys without the quotation marks. if that's not working use var_dump() to check for variable type and content.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Somewhere else you are declaring $cityName as a string and not a variable.

      Post the full code using code tags.

      Comment

      • samatair
        New Member
        • Nov 2007
        • 61

        #4
        I found which caused the error.
        I have used a variable name similar to the array name $cityName that caused the confusion.

        [PHP]$cityName = 'Dallas';
        $cityName["$i"]["$j"] = $row2['city'];
        [/PHP]
        first I have used the $cityName as a string and then as an array which caused the error. I did not realize this because the above two lines were in different files and was included in another file. I think I should keep more appropriate names depending upon the file and context.

        Comment

        • samatair
          New Member
          • Nov 2007
          • 61

          #5
          Originally posted by Markus
          Somewhere else you are declaring $cityName as a string and not a variable.

          Post the full code using code tags.
          Oh! Thanks Markus. I did not see the post of yours. But anyhow I got it sorted out. great relief. Thank you very much!

          and thank you too Dormilich

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by samatair
            I found which caused the error.
            I have used a variable name similar to the array name $cityName that caused the confusion.

            [PHP]$cityName = 'Dallas';
            $cityName["$i"]["$j"] = $row2['city'];
            [/PHP]
            first I have used the $cityName as a string and then as an array which caused the error. I did not realize this because the above two lines were in different files and was included in another file. I think I should keep more appropriate names depending upon the file and context.
            As I said above. :)

            Comment

            Working...