How to fix associative array keys that lack single quotation marks in multiple files


I've been assigned a project that have associative arrays without single quotes all over the place.



Which should be:


This generates a PHP notice and it's very bad practice.

Quoting from <a href="http://www.php.net/manual/en/language.types.array.php" rel="nofollow">PHP.net</a>:


This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.


And it's not good for performance because it has to search for a constant, before using 'key'

So I was wondering if someone knows a script that can achieve this without having to do it manually in a one by one basis for every file.


I use Vim as an editor, and I know how to use its search and replace command in a single file, however I see two problems with this:


I can't search and replace something like this because I don't think it supports regular expressions.

</li> <li>

It only works in the current opened file (as far as I know).

</li> </ol>


Does your text editor not support global search and replace using regular expressions?

I might search for something like:


and replace with


Regular expression search+replace is really useful for broad changes to lots of source files. Although make sure you take a snapshot of your code before you make the global change and verify afterwards that everything you wanted changed was changed with no unintended side-effects.


Based on OP's update that s/he is using vim, see the accepted answer of this post: <a href="https://stackoverflow.com/questions/3225477/apply-regular-expression-substitution-globally-to-many-files-with-a-script" rel="nofollow">Apply regular expression substitution globally to many files with a script</a> which describes using regular expression search/replace on multiple files in vim.

The sed-like syntax suggests to me that the command you are looking for (untested) is:

:args **/*.php | argdo %s/(\$[a-zA-Z0-9]+\[)([^\]]+)\]/\1'\2'/ge | update

You might need to fiddle with that a bit, so make a backup before you go modifying all your files. I recommend trying to find a more user-friendly IDE as well.


Does you IDE do a search and replace? If so use that. It might take some time but you need to be careful with just changing all of them in one go.


This would be another possible solution with vim:


This would change all instances of:




And will prompt for confirmation.

The following examples would remain unchanged (as they should be):

[$foo] ["foo"] ['foo'] [123]


Use an IDE to do a find and replace on the project. Most of them allow you to put a regex in the search string, so whenever it matches the pattern replace [ with [' and ] with '].


  • Creating a table in “MSWord” document out of a text in a .txt file
  • MSVC10 /MP builds not multicore across folders in a project
  • How to check whether the file exist in HDFS location, using oozie?
  • How can I pass complex expression to parametrized active pattern?
  • Installing ocropus-0.4.4
  • JS Switch case not working correctly always default is executed
  • Mongodb update() vs. findAndModify() performace
  • It is possible use the same sql azure instance from two different cloud service of two different sub
  • What is this strange character in chrome's resource css viewer?
  • How to repeat sections of a SQL query across UNIONs? (DRY in SQL)
  • EntLib Way to Bind “Null” Value to Parameter
  • Function calls are not supported. Consider replacing the function or lambda with a reference to an e
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • how does System.Web.HttpRequest::PathInfo work?
  • Django model inheritance, filtering models
  • How to revert to previous XCode version?
  • Test if a set exists before trying to drop it
  • Row Count Is Returning the incorrect number using RaptureXML
  • Chrome doesn't support silverlight anymore? How to solve this?
  • Google Custom Search with transparent background
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • DomPDF {PAGE_NUM} not on first page
  • Why value captured by reference in lambda is broken? [duplicate]
  • Insert into database using onclick function
  • Javascript simulate pressing enter in input box
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • What is Eclipse's Declaration View used for?
  • Is possible to count alias result on mysql
  • Can I make an Android app that runs a web view in Chrome 39?
  • Importing jscolor library in angular 2
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • A cron job substitute?
  • Python: how to group similar lists together in a list of lists?
  • Benchmarking RAM performance - UWP and C#
  • Understanding cpu registers
  • Why joiner is not used after Sequence generator or Update statergy
  • Recursive/Hierarchical Query Using Postgres
  • UserPrincipal.Current returns apppool on IIS