How to flip array keys and values in the code editor?

insolita

Insolita

Posted on November 2, 2020

How to flip array keys and values in the code editor?

Easy, with regex search!

  • Press Ctrl+R for show replace bar
  • Enable regex mode (button looks like .* near search field)
  • Use regex in search field: '(.+)'\s=>\s'(.+)',
  • Use expression for replace: '$2' => '$1',

You can see a tooltip with a value that will be after replace

Alt Text

It is possible even you have different quotes and spaces between arrow

Use regex
(?:'|")(.+)(?:'|")\s{0,}=>\s{0,}(?:'|")(.+)(?:'|"),

And the same replace expression '$2' => '$1',

Alt Text

Named capture groups also available:

Use regex
(?:'|")(?<src>.+)(?:'|")\s{0,}=>\s{0,}(?:'|")(?<tgt>.+)(?:'|"),

And the same replace expression '${tgt}' => '${src}',

💖 💪 🙅 🚩
insolita
Insolita

Posted on November 2, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related