【问题标题】:PHP array value replacement with recursive function inside a foreach在foreach中使用递归函数替换PHP数组值
【发布时间】:2018-09-14 06:52:27
【问题描述】:

我有一个名为 fruits 的数组。在这个数组中,如果我有oranges,我会将其更改为I like,之后任何其他橙子都会通过递归函数出现更改值。

到目前为止我已经想出了这个=>

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

}

这不会对$fruits 数组进行任何更改。即使我像这样使用递归函数,$fruits=>

内部也没有任何变化
function rec_fruits(&$arr, $i)

在 foreach 中使用而不将其分配给 $fruits 数组 =>

rec_fruits($fruits, 0);

我知道有办法实现这一点,但我想这样做。

我想在数组中最终喜欢这个 =>

数组(

[0] => 苹果

[1] => 我喜欢

[2] => 苹果

[3] => 葡萄

[4] => 西瓜

[5] => 葡萄)

【问题讨论】:

  • $i++; 返回 rec_fruits($arr, $i );
  • 如果eval.in/1057556 不是预期结果,请在问题中显示您想要得到的结果

标签: php arrays recursion


【解决方案1】:

好吧,如果您一直想知道为什么它没有对 Array 进行任何更改,那是因为以下几行:

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

所以我所做的只是为了减轻痛苦,我用传统的老式 for 循环替换了 foreach 循环。现在代码如下所示,大家都很高兴:

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

答案如下:

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

希望这对您有所帮助!

【讨论】:

  • 还有一件事,我没有对您的递归函数进行任何评估,因为我发现了上述脚本的早期代码行的错误。似乎它完全符合您的预期!
【解决方案2】:

不确定你想要完成什么。但是当调用rec_fruits 时,count($arr) &gt; $i 没有返回任何内容,因此您将null 返回到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;
    }

}

【讨论】:

  • 是的!它修复了错误,但仍然没有给出我想要的结果。函数的结果需要分配给原始数组。我也更新了问题
【解决方案3】:

递归方法示例

$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

【讨论】:

  • 问题是我需要使用 foreach 并在其中使用递归函数
猜你喜欢
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
相关资源
最近更新 更多