调试递归 while 循环 (php)
我正在编写一个函数来从小胡子模板中获取标签并生成哈希(这样做的原因是能够获取任何给定的模板并快速向开发人员显示预期的变量是什么)。
我将标签提取到平面数组中(足够简单),但下一步很棘手 - 我需要将平面数组转换为多维数组以指示嵌套变量。
这是我的示例平面数组:
$arr = array(
'one',
'#two',
'sub1',
'sub2',
'/two',
'three'
);
以及预期的输出:
$newArray = array(
'one'=>'',
'two'=>array(
'sub1'=>'',
'sub2'=>''
),
'three'=>''
);
我已经接近了,但还没有完全实现。我认为递归函数将是可行的方法(尽管我愿意接受不同的解决方案)。到目前为止,这是我所得到的:
function recurse($array, $i = 0) {
$nested = array();
while ($i < count($array)):
$tag = $array[$i];
if (preg_match('/\//',$tag)) {
return $nested;
} elseif (preg_match('/^#/',$tag)) {
$tag = str_replace('#','',$tag);
$nested[$tag] = recurse($array, $i+1);
$i+= count($nested[$tag])+1;
} else {
$nested[$tag] = '';
$i++;
}
endwhile;
return $nested;
}
我认为错误可能是它遇到了第一个“if”并从函数中一路返回,但我不确定,也不知道如何修复它。
I'm writing a function to take the tags from a mustache template and generate a hash (the reason for this is to be able to take any given template and quickly show a developer what the expected variables are).
I extract the tags into a flat array (easy enough), but the next step is tricky - I need to turn the flat array into a multi-dimensional array to indicate nested variable.
Here's my sample flat array:
$arr = array(
'one',
'#two',
'sub1',
'sub2',
'/two',
'three'
);
And the expected output:
$newArray = array(
'one'=>'',
'two'=>array(
'sub1'=>'',
'sub2'=>''
),
'three'=>''
);
I have been getting close, but am not quite there yet. I thought a recursive function would be the way to go (though I am open to a different solution). Here is what I have so far:
function recurse($array, $i = 0) {
$nested = array();
while ($i < count($array)):
$tag = $array[$i];
if (preg_match('/\//',$tag)) {
return $nested;
} elseif (preg_match('/^#/',$tag)) {
$tag = str_replace('#','',$tag);
$nested[$tag] = recurse($array, $i+1);
$i+= count($nested[$tag])+1;
} else {
$nested[$tag] = '';
$i++;
}
endwhile;
return $nested;
}
I think the bug may be that it hits the first 'if' and returns all the way out of the function, but I'm not certain, nor am I sure how to fix it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是为了好玩,我决定为您制作一个无递归并使用引用代替(比递归更有效,将数组元素别名存储在堆栈上)。也适用于嵌套子集。
输出:
您可以忽略在输出中看到的事实
&数组
代替简单的数组
。这表示在符号表中该特定元素的引用计数>。 1.原因是
$stack
仍然维护着一个引用。如果您在返回输出之前执行unset($stack);
,则附加引用将被删除,输出中的&
将消失。Just for fun I decided to make you one without recursion and using references instead (more efficient that recursion, storing array element aliases on a stack). Works with nested subsets too.
Output:
You can ignore the fact in the output you see
& array
in places instead of simplyarray
. This signifies that in the symbol table the reference count for that particular element is > 1.The reason for this is that
$stack
is still maintaining a reference. If you do anunset($stack);
before returning the output, the additional references are removed and the&
s in the output will disappear.我对你的函数进行了一些修改以满足你的需求,看看它是否适合你:
你对 $i 有一些问题...我将它作为参考,以便它会自动随函数系统更新,并使用另一个参数与下一个结束标记完全匹配...,以便它有效。
I modified your function a bit to match your needs, see if it works for you:
You had some issues with that $i... I gave it as reference so that it will automatically update with the function system, and used another parameter to match exactly the next closing tag..., so that it will validate.
是的,递归函数就是这样。一些建议:
这是做你想做的事情的另一种方法:
Yes, recurse function is the way. Some advices :
Here an other way to do what you want to :
这可能更符合您的需求(并且更接近真正的递归),但我没有测试它,因为我目前没有可以使用的 PHP 实例
用法:
步骤:
:
This might be more of what you are looking for (and a little more closer to true recursion), but I didn't test it because I don't have a PHP instance to work off of at the moment
Usage:
Steps:
Code:
关闭一个错误
当您返回嵌套数组时,您必须跳过结束标记,因此它应该是
$i += count($nested[$tag]) + 2;
。Off by one error
When you return the nested array, you have to skip over the closing tag, so it should be
$i += count($nested[$tag]) + 2;
.