How can I replace a sub string with some other string for all items of an array in PHP?
I don't want to use a loop to do it. Is there a predefined function in PHP that does exactly that?
How can I do that on keys of array?
How can I replace a sub string with some other string for all items of an array in PHP?
I don't want to use a loop to do it. Is there a predefined function in PHP that does exactly that?
How can I do that on keys of array?
Why not just use str_replace without a loop?
$array = array('foobar', 'foobaz');
$out = str_replace('foo', 'hello', $array);
$array = array_map(
function($str) {
return str_replace('foo', 'bar', $str);
},
$array
);
But array_map
is just a hidden loop. Why not use a real one?
foreach ($array as &$str) {
$str = str_replace('foo', 'bar', $str);
}
That's much easier.
This is a very good idea that I found and used successfully:
function str_replace_json($search, $replace, $subject)
{
return json_decode(str_replace($search, $replace, json_encode($subject)), true);
}
It is good also for multidimensional arrays.
If you change the "true" to "false" then it will return an object instead of an associative array.
Source: Codelinks
I am not sure how efficient this is, but I wanted to replace strings in a big multidimensional array and did not want to loop through all items as the array structure is pretty dynamic.
I first json_encode
the array into a string.
Replace all the strings I want (need to use preg_replace
if there are non-English characters that get encoded by json_encode
).
json_decode
to get the array back.
function my_replace_array($array,$key,$val){
for($i=0;$i<count($array);$i++){
if(is_array($array[$i])){
$array[$i] = my_replace_array($array[$i],$key,$val);
}else{
$array[$i]=str_replace($key,$val,$array[$i]);
}
}
return $array;
}
With array_walk_recursive()
function replace_array_recursive( string $needle, string $replace, array &$haystack ){
array_walk_recursive($haystack,
function (&$item, $key, $data){
$item = str_replace( $data['needle'], $data['replace'], $item );
return $item;
},
[ 'needle' => $needle, 'replace' => $replace ]
);
}
There is no predefined function anymore (since PHP 8.1). So this will replace in all elements, even if there are Objects:
<?php
class Arrays {
/**
* Replaces in all strings within a multidimensional array, even if objects are included
* @param string $search search for this string
* @param string $replace_with replace with this string
* @param mixed $haystack Array or Object to search in all elements
* @return mixed gives the same structure back
*/
public static function replace_everywhere($search, $replace_with, $haystack) {
if (is_array($haystack) or is_object($haystack)) {
// Arrays and Objects
foreach ($haystack as &$value) {
$value = Arrays::replace_everywhere($search, $replace_with, $value);
}
return $haystack;
} elseif (is_string($haystack)) {
// replace in a string element
return str_replace($search, $replace_with, $haystack);
} else {
// other datatypes (e.g. integer) stay untouched
return $haystack;
}
}
}
// You can call this like e.g.
$a = array(true, 1, 'foo<bar', 'foo<baz', array("foo<loo"), (object) array('1' => 'foo<boo'));
$a = Arrays::replace_everywhere("<", "<", $a);
var_export($a);
Example: https://3v4l.org/CDGvC#v8.2.5
If you want to replace array keys, I think that's another question with good answers like: recursively-change-keys-in-array
$base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), );
$replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));
$basket = array_replace_recursive($base, $replacements);
$basket = array_replace($base, $replacements);