title: PHP array value replacement with recursive function inside a foreach
body: <p>I got a array called fruits. In this array if i have <code>oranges</code> I will change it to <code>I like</code> and after that any other oranges comes up change value with a recursive function.</p>
  <p>So far I have come up with this =&gt;</p>
  <pre><code>$fruits = array(&quot;apple&quot;, &quot;orange&quot;, &quot;apple&quot;, &quot;orange&quot;, &quot;watermelon&quot;, &quot;orange&quot;);
  
  foreach($fruits as $key=&gt;$fruit){
  
      if($fruit == &quot;orange&quot;){
          $fruits[$key] = &quot;I like&quot;;
          $fruits = rec_fruits($fruits, 0);
      }
  
  }
  
  function rec_fruits($arr, $i){
      if(count($arr) &gt; $i ) {
          if($arr[$i] == &quot;orange&quot; ){
  
              $arr[$i] = &quot;grape&quot;;
  
          }
  
          $i++;
          return rec_fruits($arr, $i );
      } else {
  
          return $arr;
      }
  
  }
  </code></pre>
  <p>This is not making any changes to the <code>$fruits</code> array. Even when I use the recursive function like this nothing is changed inside the <code>$fruits</code>=&gt;</p>
  <pre><code>function rec_fruits(&amp;$arr, $i)
  </code></pre>
  <p>Usage in the foreach without assigning it  to <code>$fruits</code> array =&gt;</p>
  <pre><code>rec_fruits($fruits, 0);
  </code></pre>
  <p>I know there are ways to achieve this, but i wanted to do like this.</p>
  <p>What i want in the array to finally like this =&gt;</p>
  <blockquote>
  <p>Array (</p>
  <p>[0] =&gt; apple</p>
  <p>[1] =&gt; I like</p>
  <p>[2] =&gt; apple</p>
  <p>[3] =&gt; grape</p>
  <p>[4] =&gt; watermelon</p>
  <p>[5] =&gt; grape )</p>
  </blockquote>