In PHP how do i update values in an asssociative array and store the entire array?

Posted by amnesia-55 on Stack Overflow See other posts from Stack Overflow or by amnesia-55
Published on 2010-03-11T18:58:44Z Indexed on 2010/03/11 19:04 UTC
Read the original article Hit count: 166

Filed under:

Here's a code example:

  $array = array();

  $array['master']['slave'] = "foo";

  foreach ($array as $key => $value) {

    foreach ($value as $key2 => $value2) {

      if (preg_match('/slave/',$key2)) {

        $value[$key2] = "bar";
        print "$value[$key2] => $key2 => $value2\n";
      }
    }
  }

  print_r($array);

Output:

bar => slave => foo

Array

(

    [master] => Array

        (
            [slave] => foo
        )

)

Rather i would like to have the following as the final array:

Array
(

    [master] => Array

        (
            [slave] => bar
        )

)

What wrong am i doing here?

Thank you!

© Stack Overflow or respective owner

Related posts about php