How do I automate the conversion of a PHP array from one format to another, in VS Code?

Solution 1:

use REGEX when you need to restructure data from one to another.

I don't know about Visual Studio, but most IDE or Notepads(i.e Notepad++) allow you to search and replace, including applying regex.

Since the structure of your data is the same you can write something very simple to help you. Now, I am not a regex expert (just know basics so I can use it to help my daily routine) so below is very crude but will do what you are looking for and will give you an idea.

  1. Go to Search and Replace and enable use regex option.

Find:

'([A-Z]+)'=>array\('name'=>'([A-Z]+)','code'=>'([0-9]+)'\),

Replace with:

['code' => '$1', 'dialcode' => '$3'],

What happening here is we have 3 (groups), the other bit is a structure with some escaped characters as regex is sensitive for many symbols as they have a function.

Anyway, from that 3 groups we need only 2, and to get their content, in your replace field you add $1, $2 and $3 where you need the content of a group to be. The number after $ sign represents the order of a group.