2

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??

2 Answers 2

2

I just slightly modified my code I answered your previous question with.

Added a foreach to loop the words.

$rule = 
[
"c" => "d",
"a" => "o",
"t" => "g",
"h" => "a",
"1" => "@",
"e" => "n",
"n" => "t"
];
$orders = ['cat1hen','cathen','hencat'];

foreach($orders as $order){
    $arr = str_split($order);

    $str ="";
    foreach($arr as $key){
        $str .= $rule[$key];
    }

    $str = preg_replace("/(.*?)(@)(.)(.*)/", "$1$3$2$4", $str);
    echo $str . "\n";
}

//doga@nt
//dogant
//antdog

https://3v4l.org/LhXa3

Sign up to request clarification or add additional context in comments.

2 Comments

Why use foreach if strtr does the same?
Didn't think about that.
0

As another solution without regexps, you can wrap your array_walk into a function and map it to each element of array:

function myFunction($sourceString)
{
    $rule =
        [
            "c" => "d",
            "a" => "o",
            "t" => "g",
            "h" => "a",
            "1" => "@",
            "e" => "n",
            "n" => "t"
        ];

    $keys = str_split($sourceString);

    $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);
    return implode($output);
}

print_r(array_map('myFunction', ['cat1hen','cathen','hencat']));

Sample fiddle.

4 Comments

You missed the part (from last question) about the @ sign should move place.
@Andreas His code also move the position of @ which is handeled by array_walk.
This is old commentary by @Andreas to my previous answer.
@u_mulder Thank you for simplifying answer.It is easy to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.