I have a multidimensional array like the following:
Array (
[results] => Array (
[0] => Array (
[object_id] => 13
[id] => 13
[idno] => e00110-o00005-2010-PROG
[display_label] => La Bohème / PUCCINI - 2010
[ca_objects.description] => Libreto de Luigi Illica y Giuseppe Giacosa basado en Escenas de la vida bohemia de Henri Murger Nueva producción – Teatro Colón
[ca_objects.type_id] => Programa de mano
)
//more data here
I'm trying to loop the array and replace "object_id" key for "new_id" using str_replace.
$str="object_id";//The string to search for
$rep="new_id";//The replacement string
foreach ($array as $value) {
foreach ($value as $key2 => $value2) {
foreach ($value2 as $key3 => $value3) {
str_replace($str,$rep,$key3);
echo $key3." : ".$value3."<br>"; //It gets printed with no changes
}
}
}
The above code does not work, can you see what am I doing wrong?. I tried using strings instead of variables but didn't work either. Thanks in advance.
str_replace()returns the new string, it doesn't modify the string in place.str_replace()is for doing multiple replaces of substrings, but in your example the match is exact for the whole key. Do you need to do substring replacement?