A Stefan Klopp Weblog
Random header image... Refresh for more!

Converting bad associative arrays

Recently after doing an upgrade of PHP I noticed all my scripts to be breaking, and after doing a little bit of research I realized that they were breaking because I was calling my associative arrays (hashes) wrong. I was still using an old style in which you don’t put quotes around the key value of the array.

Thus an example of the bad way to do associative arrays is:

  • $array[key]
  • And the proper way is:

  • $array['key']
  • This used to work fine, however it looks like PHP is phasing this notation out (as they rightfully should) , thus in the latest version of PHP (4.3.10) you will start to see errors for such a notation.

    Now you might say “what can I do?” you don’t want to have to go through all your code looking up associative arrays finding where you did the wrong notation. Thus I came up with a fancy pancy regular expression that will search and replace all associative arrays that have been written under the old notation and correct them. You can use this regex with sed if you like, that is what I did. Actually I wrote a script that went through all my directories recursively and corrected all my files. If you would like a copy of that just ask. Anyway without further delay here is the wonderful regular expression:

    s/($([a-zA-Z_])*( *-> *[a-zA-Z_]*)*([))([a-zA-Z][a-zA-Z_]*)(])/1′5′6/g

    Related Posts

    • No Related Post

    0 comments

    There are no comments yet...

    Kick things off by filling out the form below.

    Leave a Comment