How can I recursively search for and replace values inside of an unknown-depth multidimensional PHP array?
<p>I'm working with a JSON string. I'm converting it to an associative array to find specific values and change those values when a certain key is found (['content']). The depth of the array is always unknown and will always vary.</p>

<p>Here is the function I wrote. It takes an array as an argument and passes it by reference so that the variable itself is modified rather than a copy of it scoped locally to that function.</p>

<pre><code>$json_array = json_decode($json_string, true);

function replace_data(&amp;$json_array, $data='REPLACE TEST')
{
   foreach($json_array as $key =&gt; $value) {
       if ($key == 'content' &amp;&amp; !is_array($value)) {
           $json_array[$key] = $data;

       } else {
           if (is_array($value)) {
               replace_data($value, $data);
           }
       }
   }

}


replace_data($json_array, "test test test");
var_dump($json_array);
</code></pre>

<p>What I'm expecting to happen is every time a key ['content'] is found at no matter what depth, it replaces with that value specified in the $data argument.</p>

<p>But, when I <code>var_dump($json_array)</code> Those values are unchanged. </p>

<p>What am I missing?</p>