php - 合并两个数组并替换它们

php  - 合并两个数组并替换它们

问题描述:

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

我想合并两个数组并用 strtr code>函数替换文本。 p >

我之前使用过这个 p>

  $ text =“cat cow”; 
 $ array = array(
“cat”=>  “dog”,
“cow”=>“bull”
); 
 $ output = strtr($ text,$ array); 
  code>  pre> 
 
 

这返回 dog bull code> ... p>

现在我有两个这样的数组... p>

  $  a = array(“cat”,“dog”); 
 $ b = array(“dog”,“bull”); 
  code>  pre> 
 
 

两个数组都将 有值替换 p>

现在,我如何将它们组合并替换? 我尝试了 $ array = $ a + $ b code>和 array_combine code>,但它们没有用... p>

请帮忙。 .. p> div>

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.