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