I want to merge two arrays and replace the text with strtr
function.
I was using this before
$text = "cat cow";
$array = array(
"cat" => "dog",
"cow" => "bull"
);
$output = strtr($text, $array);
this returned dog bull
...
Now I have two arrays like this...
$a = array("cat", "dog");
$b = array("dog", "bull");
Both the arrays will have values to replace
Now, how do I combine them and replace? I tried $array = $a + $b
and array_combine
, but they didn't work...
Please Help...
I think two arrays must be
$a = array("cat", "cow");
$b = array("dog", "bull");
And you can use
$c = array_combine($a, $b);
$output = strtr($text, $c);
Do you mean merge them to get array('cat','dog','bull')? If so just do:
$array = array_unique(array_merge($a,$b));
I dont know how you have tried.
$text = "cat cow";
$array = array(
"cat" => "dog",
"cow" => "bull"
);
$text = "cat cow";
$array = array("cat" => "dog",
"cow" => "bull"
);
$output = strtr($array, $array);
echo $output;
//output -> dog bull
$a = array("cat", "cow");
$b = array("dog", "bull");
$c = array_combine($a,$b);
print_r($c);
$output1 = strtr($text, $c);
echo $output1;
//output -> dog bull
I thing the above code gives you what output you need.
I think you have used the wrong array Check the $a and $b array I hope i have helped you.