menaka
menaka

Reputation: 1068

PHP array value replacement with recursive function inside a foreach

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 )

Upvotes: 0

Views: 156

Answers (3)

Randika Vishman
Randika Vishman

Reputation: 8124

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!

Upvotes: 1

Farooq Ahmed Khan
Farooq Ahmed Khan

Reputation: 4094

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

Upvotes: 0

DarkBee
DarkBee

Reputation: 15625

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;
    }

}

Upvotes: 1

Related Questions