0

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 )

asked Sep 14, 2018 at 6:52
2
  • $i++; return rec_fruits($arr, $i ); Commented Sep 14, 2018 at 7:06
  • if eval.in/1057556 is not expected result, show in the question, what you want to get Commented Sep 14, 2018 at 12:23

3 Answers 3

1

Well, if you have been wondering why the heck it doesn't make any changes to the Array then it's because of following lines:

$fruits = array("apple", "orange", "apple", "orange", "watermelon", "orange");
// Note: Your above array is not an associative array which contains Key-Value pairs
// But here in the bellow foreach-loop you are trying to instantiate a Key-Value
// pair for each of the element in the given array
foreach($fruits as $key=>$fruit){
 // So it doesn't even touch following lines and it doesn't go for the recursive function call too :D
 if($fruit == "orange"){
 $fruits[$key] = "I like";
 $fruits = rec_fruits($fruits, 0);
 }
}

So what I did is for all the ease of pain, I replaced the foreach-loop with traditional old-school for-loop. Now the code looks as following and everybody are happy:

<?php
$fruits = array("apple", "orange", "apple", "orange", "watermelon", "orange");
for($i = 0; count($fruits) > $i; $i++){
 if($fruits[$i] == "orange"){
 $fruits[$i] = "I like";
 $fruits = rec_fruits($fruits, 0);
 }
}
echo json_encode($fruits);
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;
 }
}

The answer is as follows:

["apple","I like","apple","grape","watermelon","grape"]

Hope this was helpful to you!

answered Sep 19, 2018 at 18:57

1 Comment

And one thing, I didn't evaluate anything about your Recursive function, since I found the bug earlier code lines of your above script. Seems like it does what you exactly intended it to do for you!
1

Not sure what you want to accomplish. But when calling rec_fruits you are not returning anything when count($arr) > $i, therefor your are returning null to fruits, which then causes the notice of the undefined index.

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;
 }
}
answered Sep 14, 2018 at 7:08

1 Comment

Yeah! it fixed the error , but it still not give out the results i wanted. What ever the results from the function need to assign to the original array. I also updated the question
0

A sample recursive approach

$array = array("apple", "orange", "pear", "orange", "apple", "orange", "orange");
$key = "orange";
$value = "I like it";
// array_search will return index of the value if it exists in mentioned source 
// empty actually check for a FALSE value.
while(!empty($index = array_search($key, $array))) { 
 $update = array($index => $value); 
 $array = array_replace($array, $update);
}
print_r($array); // all oranges are replaced with your
answered Sep 14, 2018 at 7:07

1 Comment

the thing is what I need to use a foreach and inside it a recursive function

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.