teckfolls teckfolls - 9 months ago 49
PHP Question

Replace from array with other array

How can I replace arrays from other arrays?

My code:

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);

The result of
need to be replace if a array is on the list:

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

I want it like:

$text_to_draw = "Cat, Facebook, Fire";

Then the output should be:

Mouse, Facebook, Fire

Answer Source

str_replace() accepts arrays:

$output = str_replace(array_keys($text_replace), $text_replace, $text_to_draw);
  • Search for the array keys
  • Replace with the array values