2

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.

7
  • 1
    str_replace() returns the new string, it doesn't modify the string in place. Commented Jun 19, 2013 at 23:08
  • @Barmar Thanks! Silly me. I'll keep on researching then. Commented Jun 19, 2013 at 23:10
  • Do you actually want to modify the keys in the array, or just print the elements with the modified keys? Commented Jun 19, 2013 at 23:10
  • @Barmar I want to modify them. Commented Jun 19, 2013 at 23:13
  • 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? Commented Jun 19, 2013 at 23:17

3 Answers 3

1

...if you really want to use str_replace():

$array['results'] = array_map(function($item){
  $keys = implode(',', array_keys($item));
  $keys = str_replace('object_id', 'new_id', $keys);
  return array_combine(explode(',', $keys), array_values($item));

}, $array['results']);

The other way - create a new array, then iterate over the old array and assign values from it to the new array, while changing the keys you want:

$array['results'] = array_map(function($item){
  $item['new_id'] = $item['object_id'];      
  unset($item['object_id']);
  return $item;

}, $array['results']);

(this one will reorder the array, if it matters)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I like the new array aproach!
1
foreach ($array as &$value) {
  foreach ($value as $key2 => &$value2) {
    $value2[$rep] = $value2[$str];
    unset($value2[$str]);
  }
}

It's necessary to iterate over the arrays using references so that the modifications affect the original array, not a copy.

2 Comments

Thanks! I'll try the code. And thanks for the useful information!
I tried and I ended with an array without the key. Like this Array ( [results] => Array ( [0] => Array ( [id] => 13 [idno] => e00110-o00005-2010-PROG [display_label]// etc. I'll try to debug it in order to learn. Thanks again!
0

@One Trick Pony: I followed your suggestion and created a new array. It was exactly what I needed and I did it by myself!!. Following code creates the new array. Thank you all so much for helping me!

 $display=array();
    foreach ($array as $value) {
      foreach ($value as $value2) {
        $display [] = array (
                         'Id del objeto' => $value2['object_id'],
                         'Título' => $value2['display_label'],
                         'Descripción' => $value2['ca_objects.description'],
                         );
              }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.