In PHP how do i update values in an asssociative array and store the entire array?
<p>Here's a code example:</p>

<pre><code>  $array = array();

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

  foreach ($array as $key =&gt; $value) {

    foreach ($value as $key2 =&gt; $value2) {

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

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

  print_r($array);
</code></pre>

<p>Output:</p>

<pre><code>bar =&gt; slave =&gt; foo

Array

(

    [master] =&gt; Array

        (
            [slave] =&gt; foo
        )

)
</code></pre>

<p>Rather i would like to have the following as the final array:</p>

<pre><code>Array
(

    [master] =&gt; Array

        (
            [slave] =&gt; bar
        )

)
</code></pre>

<p>What wrong am i doing here?</p>

<p>Thank you!</p>

<p><strong>Note:
Example2:</strong></p>

<pre><code>enter code here $a= array('l1'=&gt;array('l2'=&gt;array('l3'=&gt;array('l4'=&gt;array('l5'=&gt;'foo')))));
</code></pre>

<p>$a['l1']['l2']['l3']['l4']['l5'] = 'bar';</p>

<p>foreach ($a as $i => &amp;$values) {</p>

<p>foreach ( $values as $key => &amp;$value) {</p>

<pre><code>if (is_array($value)){

  print_array($value,$key);

}
</code></pre>

<p>}</p>

<p>}</p>

<p>function print_array ($Array, $parent) {</p>

<p>foreach ($Array as $i1 => &amp;$values1) {</p>

<pre><code>if (is_array($values1)){

  foreach ($values1 as $key1 =&gt; &amp;$value1) {

    if (is_array($value1)) {

      print_array($value1,$values1);

    }

    else {

      print "       $key1 =&gt; $value1\n";

    }

  }

}

else {

  if (preg_match('/l5/',$i1)) {

    $values1 = "foobar";

    print "       $i1 =&gt; $values1\n";

  }

}
</code></pre>

<p>}</p>

<p>}</p>

<p>print_r($a);</p>

<p>Output does not reflect 'foobar' in l5</p>

<p>Thank you!</p>
