PHP递归函数不返回值 [英] PHP Recursive function not returning value

查看:124
本文介绍了PHP递归函数不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我错过了什么吗?在试图实现一个算法时,我无法获得函数的返回值。最初尝试返回一个数组我遇到简单的类型也不行。



注意:这是一个简化版本来说明这一点。我知道对于图示的代码,可以使用类似print_r(array_map(function($ x){return $ x + 2;},array(2,4,6,8)));但这不会在完整的问题。



并为问题。我有以下代码:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' )){
return $ accumulator;
} else {
recursive1(array_slice($ myArray,1),$ accumulator + 1);
}
}
$ testArray = array(3,5,7,9);
print( - >。recursive1($ testArray,10)。< -\\\
);

取得零结果



<$ p $ (输出)
- >< -

添加一点看内部我有

  function recursive1($ myArray,$ accumulator){
if(empty ($ myArray)){
print([。$ accumulator。] \\\
); //最终结果
返回$ accumulator;
} else {
print(<。$ accumulator。>); //部分结果
recursive1(array_slice($ myArray,1),$ accumulator + 1);
}
}
$ testArray = array(3,5,7,9);
print( - >。recursive1($ testArray,10)。< -\\\
);

我得到它正在执行它所做的功能,但返回值丢失。 10 $ 12 $ 13



$ $ b - >< -

所以我应该有

 (输出)
- > 14 < -

顺便说一下,我正在使用

 > php -version 
PHP 5.5。 30(cli)(构建时间:2015年10月23日17:21:45)
版权所有(c)1997-2015 The PHP Group
Zend Engine v2.5.0 Copyright(c)1998-2015 Zend Technologies

我也试着将这个值赋给一个变量(在函数之外)无效。 p>

有什么想法? : - )

解决方案

您应该拥有这个:

  else {
print(<。$ accumulator。>); //部分结果
返回recursive1(array_slice($ myArray,1),$ accumulator + 1);

$ / code>

没有 return 声明,您的最终结果永远不会返回。


Am I missing something? In trying to implement an algorithm I can't obtain a return value for a function. Originally trying to return an array I've encountered simple types won't work as well.

NOTE: This is a simplified version to illustrate the point. I'm aware that for the illustrated code one could use something like print_r(array_map(function($x){return $x+2;}, array(2, 4, 6, 8))); but this won't do in the full problem.

And for the question. I have the following code:

function recursive1($myArray, $accumulator){
  if(empty($myArray)){
    return $accumulator;
  } else {
    recursive1(array_slice($myArray, 1), $accumulator+1);
  }
}
$testArray = array(3, 5, 7, 9);
print("->".recursive1($testArray,10)."<-\n");

Getting nil as a result

(output)
-><-

Adding a little to see the internals I have

function recursive1($myArray, $accumulator){
  if(empty($myArray)){
    print("[".$accumulator."]\n");   // End result
    return $accumulator;
  } else {
    print("<".$accumulator.">");   // partial results
    recursive1(array_slice($myArray, 1), $accumulator+1);
  }
}
$testArray = array(3, 5, 7, 9);
print("->".recursive1($testArray,10)."<-\n");

I get that the function it's doing what it suppose to do but the return value is lost.

(output)
<10><11><12><13>[14]
-><-

So I should have

(output)
->14<-

By the way I'm using

>php -version
PHP 5.5.30 (cli) (built: Oct 23 2015 17:21:45)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0 Copyright (c) 1998-2015 Zend Technologies

I've also tried to assign the value to a variable (outside the function) to no avail.

Any ideas? :-)

解决方案

You should have this:

else {
   print("<".$accumulator.">");   // partial results
   return recursive1(array_slice($myArray, 1), $accumulator+1);
}

Without the return statement, your final result is never returned.

这篇关于PHP递归函数不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆