I have this array of countries with their dial codes:
$dial_codes = [
'AF'=>array('name'=>'AFGHANISTAN','code'=>'93'),
'AD'=>array('name'=>'ANDORRA','code'=>'376'),
// more items
];
I want/need to turn the above array into this (which is more similar to Laravel Seeder format):
$dial_codes = [
['code' => 'AF', 'dialcode' => '93'],
['code' => 'AD', 'dialcode' => '376'],
// more items
];
Question
Instead of doing this by hand, how do I automate it, in Visual studio code?
CodePudding user response:
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.
- 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.