Last active
December 13, 2023 23:29
-
-
Save vdvm/4665450 to your computer and use it in GitHub Desktop.
Recursive array str_replace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
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;
}
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
Thanks!