Here I created Hashmap array in PHP. I gave input as $keys = str_split('cat1hen') which give output 'doga@nt'.
$rule =
[
"c" => "d",
"a" => "o",
"t" => "g",
"h" => "a",
"1" => "@",
"e" => "n",
"n" => "t"
];
$keys = str_split('cat1hen');
$output = [];
array_walk($keys,
function($item, $index) use($rule,$keys, &$output) {
if($rule[$item] == '@' && isset($keys[$index + 1])) {
$output[] = $rule[$keys[$index + 1]];
return;
}
if(isset($keys[$index - 1]) && $rule[$keys[$index - 1]] == '@') {
$output[] = '@';
return;
}
$output[] = $rule[$item];
return;
},
$keys);
echo implode($output);
Instead of giving one input value,I want to give input as array with many value i.e $keys = ['cat1hen','cathen','hencat'] which should give output as ['doga@nt','dogant','antdog'].How to modify code to do so??