PHP deprecated each function replacement with foreach

PHP deprecated each function replacement with foreach


    Jun 01, 2020#1

    I've search hard for this information and could not find it. After several hours of testing and learning more about regex, I have come up with the following search/replace that saves me hours of work updating outdated PHP files with deprecated each statements when used in combination with while as a substitute for foreach.

    Looking for: 

    Code: Select all

    while(list($key,$val) = each($values))
    while(list($key1,$val1) = each($arrVals["name"]))
    Find: while\(list\(([\$a-z0-9_]+),([\$[a-z0-9_]+)\) = each\(([\$a-z0-9_\[\]\"]+)\)\)
    Replace: foreach(\3 as \1=>\2)

    Be sure to use regular expression engine Unix or Perl for this search/replace.

    *note - this will include array variables used in the each

      PHP search/replace update when array keys were not coded with quotes

      Jun 01, 2020#2

      I've come across some old PHP code where coders did not use quotes for array keys as it was not required. New PHP updates now require this. Here is my search replace code. Using Unix Regex search/replace. If you use Replace All, be sure you don't have any JavaScript code in your files since you don't use quotes with js, and could break your code.

      Looking for: $array[name]

      Find: \[([a-z0-9_]+)\]
      Replace: \["\1"\]

      Will result in: $array["name"]