递归=可怕!(Codeigniter PHP)
我一直讨厌递归,因为我的大脑无法处理无限循环,也无法找到正确思考它的方法。我想知道如何使用它打印一个值数组。我想将结果编译成一个字符串并返回,然后使用递归=可怕!(Codeigniter PHP),php,arrays,codeigniter,recursion,static-members,Php,Arrays,Codeigniter,Recursion,Static Members,我一直讨厌递归,因为我的大脑无法处理无限循环,也无法找到正确思考它的方法。我想知道如何使用它打印一个值数组。我想将结果编译成一个字符串并返回,然后使用将其正确隔开: var $count=0; static $formatted=''; function Process_Array($array) { global $count,$formatted; $count++; //this variable is for calculating tab sp
将其正确隔开:
var $count=0;
static $formatted='';
function Process_Array($array)
{
global $count,$formatted;
$count++; //this variable is for calculating tab space
if(is_array($array) === true)
{
foreach($array as $key => $value)
{
echo "<br /><br />Value=";print_r($value);
if(is_array($value) === true)
{
$this->Process_Array($value);
}
else
{
//Calculate tab spacing (level in tree).
for($i = 1 ; $i < $count ; $i++)
$formatted.=" ";
$formatted.=$value."<br />";
}
}
}
$count--;
$final=$formatted;
$formatted='';
return $final;
}
var$count=0;
静态$formatted='';
函数进程\u数组($Array)
{
全局$count,$formatted;
$count++;//此变量用于计算选项卡空间
if(is_array($array)==true)
{
foreach($key=>$value的数组)
{
echo“
Value=“;print_r($Value);
if(is_数组($value)==true)
{
$this->Process\u数组($value);
}
其他的
{
//计算制表符间距(树中的标高)。
对于($i=1;$i<$count;$i++)
$formatted.=“”;
$formatted.=$value.“
”;
}
}
}
$count--;
$final=$formatted;
$formatted='';
返回$final;
}
我试着读了很多文章,但这让我很生气。这是我下载并修改的一些代码。与{}语言相比,函数式语言(Haskell、OCaml)有一个有趣的地方,就是没有循环。迭代是通过递归完成的。所以我觉得在递归函数中有foreach循环很有趣。为什么不在所有事情上都使用循环呢 递归需要一个基本情况和一个递归步骤。如果您正在打印一个数组,一个好的基本情况可能是检查数组是否为空,然后返回。然后递归步骤将弹出第一个元素并将其打印出来
function printArray($array)
{
//base case
if(empty($array)) {
return;
}
print_r(array_pop($array));
//recursive step
return printArray($array);
}
这对您来说不是一个完整的函数,但它确实在数组的输出中添加了空间格式,以表示多维数组中元素的深度,而不使用任何全局变量 希望这将帮助您完善自己的解决方案 更新以反映以字符串形式返回输出,而不是从函数中回显
这是另一个。不过,我会对对象使用附加条件
$arr = array(1,array('2a','2b',array('3a','3b')),4,5,array(),'example','');
function Process_Array($arr, $pre_str='', $space_count=-1) {
$result = '';
$spaces = '';
for ($i = 0; $i < $space_count; $i++) $spaces .= ' ';
if (is_array($arr) && !empty($arr)) {
foreach ($arr as $key => $value) {
$result .= Process_Array($value,$pre_str,$space_count+1);
}
} else $result .= $spaces . $pre_str . (empty($arr) ? '' : $arr) . "<br />\n";
return $result;
}
echo Process_Array($arr,'Value = ');
$arr=array(1,array('2a','2b',array('3a','3b')),4,5,array(),'example','';
函数进程\u数组($arr,$pre\u str='',$space\u count=-1){
$result='';
$spaces='';
对于($i=0;$i<$space\u count;$i++)$spaces.='';
if(is_数组($arr)&&!空($arr)){
foreach($arr作为$key=>$value){
$result.=Process\u数组($value、$pre\u str、$space\u count+1);
}
}else$result.=$spaces.$pre_str.(空($arr)?“”:$arr)。“
\n”;
返回$result;
}
回波处理_数组($arr,'Value=');
产出:
值=1值=2a
值=2b
值=3a
值=3b
值=4
Value=5
Value=
Value=example
Value=
共享一个输入和所需输出的示例。递归(n)。1.参见递归。旁注:您知道
全局变量是不好的,对吗?顺便问一下,您使用的是什么版本的PHP?这里有一个链接,指向用于生成数组的方法:答案:
0: test1
1: test2
2:
0: test3
1:
0: test4
1: test5
2: test6
3: test7
$arr = array(1,array('2a','2b',array('3a','3b')),4,5,array(),'example','');
function Process_Array($arr, $pre_str='', $space_count=-1) {
$result = '';
$spaces = '';
for ($i = 0; $i < $space_count; $i++) $spaces .= ' ';
if (is_array($arr) && !empty($arr)) {
foreach ($arr as $key => $value) {
$result .= Process_Array($value,$pre_str,$space_count+1);
}
} else $result .= $spaces . $pre_str . (empty($arr) ? '' : $arr) . "<br />\n";
return $result;
}
echo Process_Array($arr,'Value = ');