$datas = [
'aasd' => 'bbsr',
'aas' => [
'zzc' => 'ffts'
]
];
$key = 's';
$value = '<SUPER>';
// Результат:
(array) [
'aa<SUPER>d' => 'bb<SUPER>r',
'aa<SUPER>' => [
'zzc' => 'fft<SUPER>'
]
];
function replace_everywhere($datas, $key, $value) {
echo $srl = var_export($datas, 1);
$rep_key = str_replace("/", "\\/", $key);
$rep_key = str_replace("'", "\\\\'", $rep_key);
$rep_value = str_replace("'", "\\'", $value);
$srl = preg_replace_callback("/([^\\\\]'.*?)$rep_key(.*?[^\\\\]'|')/",
function($m) use ($rep_value) {
return $m[1] . $rep_value . $m[2];
}, $srl);
eval("\$datas_new = $srl;");
return $datas_new;
}
$datas = [
'aasd' => 'bbsr',
'aas' => [
'zzc' => 'ffts'
]
];
var_dump(replace_everywhere($datas, 's', 'SUPER'));
Вывод:array (size=2)
'aaSUPERd' => string 'bbSUPERr' (length=8)
'aaSUPER' =>
array (size=1)
'zzc' => string 'fftSUPER' (length=8)
function str_replace_array($datas, $key, $value){
$newArr = array();
$foreach = function($valueArr, $keyArr) use(&$newArr, $key, $value){
if(is_array($valueArr)){
$keyArr = str_replace($key,$value,$keyArr);
$newArr[$keyArr] = str_replace_array($valueArr, $key, $value);
}else{
$keyArr = str_replace($key,$value,$keyArr);
$valueArr = str_replace($key,$value,$valueArr);
$newArr[$keyArr] = $valueArr;
}
};
array_walk($datas, $foreach);
return $newArr;
}