用于修改数百个文件中的php数组元素的脚本

I have some severely deprecated PHP code that I'm sifting through. One of the problems that I have is hundreds of errors like this:

[Mon Dec 09 07:00:33 2013] [error] [client 127.0.0.1] PHP Notice: Use of undefined constant id - assumed 'id' in /home/srv/site.local/content/online.php on line 171, referer: http://site.local/index.php

These result in the practice of a previous coder of calling arrays like this:

$array[some_element]

instead of:

$array['some_element']

So I know how to fix that by going through each file and adding the quotes, over, and over, and over again. My question is how to write script for this. I imagine this might be a sed or awk script, but I'm not sure. I'm going to start working on a sed as soon as I'm done posting this, but any suggestions?

I don't know anything about php, so I'm not sure whether this solution is a particularly good one:

sed  "s|\[[ ]*|\[\'|g;s|[ ]*\]|\'\]|g" test.in

example of use:

[fixarraysubscript $] cat test.in
$array[some_element]

$name1[index]

$name2[index]


$name3[ id ]

$name4[ id2 ]

[fixarraysubscript $]
[fixarraysubscript $] sed  "s|\[[ ]*|\[\'|g;s|[ ]*\]|\'\]|g" test.in
$array['some_element']

$name1['index']

$name2['index']


$name3['id']

$name4['id2']

[fixarraysubscript $]

obviously my input file is somewhat contrived. If this doesn't work for you, please feel free to post some real input.

This may work with gnu awk

echo '$array[some_element]' | awk '{print gensub(/\$([^[]+)\[([^]]+)]/,"$\\1["q"\\2"q"]","g")}' q="'"
$array['some_element']

For your file

awk '{print gensub(/\$([^[]+)\[([^]]+)]/,"$\\1["q"\\2"q"]","g")}' q="'" file

or

awk '{print gensub(/\$([^[]+)\[([^]]+)]/,"$\\1[\x27\\2\x27]","g")}' file

EDIT: Changed regex to reflect variable array name.