Skip to content

Instantly share code, notes, and snippets.

@vdvm
Last active December 13, 2023 23:29
Show Gist options
  • Save vdvm/4665450 to your computer and use it in GitHub Desktop.
Save vdvm/4665450 to your computer and use it in GitHub Desktop.
Recursive array str_replace
<?php
function recursive_array_replace($find, $replace, $array) {
if (!is_array($array)) {
return str_replace($find, $replace, $array);
}
$newArray = array();
foreach ($array as $key => $value) {
$newArray[$key] = recursive_array_replace($find, $replace, $value);
}
return $newArray;
}
@leonardola
Copy link

Thanks man, very useful!

@regiscamimura
Copy link

Thanks!

@powerbuoy
Copy link

I've used this but recently switched to this which is about 20% faster in my tests:

function recursive_array_replace ($find, $replace, $array) {
	array_walk_recursive($array, function (&$value, $key) use ($find, $replace) {
		$value = str_replace($find, $replace, $value);
	});

	return $array;
}

@herahadi
Copy link

thank you it's solved my problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment