从数组替换为其他数组

时间:2016-11-02 17:04:08

标签: php arrays replace

如何从其他阵列替换数组?

我的代码:

foreach($item_name[1] as $index => $text_to_draw) {
    $y_pos = $position_text_array[$index];
    $color_text = $color_array[$index];
    imagettftext($image, 10, 0, $x_pos, $y_pos, $color_text, $font, $text_to_draw);
}

如果数组在列表中,则需要替换$text_to_draw的结果:

$text_replace = array(
    "Nice" => "Bad",
    "Beautiful" => "Nice",
    "Fish" => "Dog",
    "Cat" => "Mouse",
);

我想要它:

$text_to_draw = "Cat, Facebook, Fire";

然后输出应为:

  

鼠标,Facebook,Fire

1 个答案:

答案 0 :(得分:0)

str_replace()接受数组:

$output = str_replace(array_keys($text_replace), $text_replace, $text_to_draw);
  • 搜索数组键
  • 替换为数组值