PHP递归语法
我以前从未做过递归,我觉得我可能遗漏了一些东西。这段代码可以工作,但似乎比它需要的更“冗长”。似乎我见过人们做PHP递归语法,php,tail-recursion,Php,Tail Recursion,我以前从未做过递归,我觉得我可能遗漏了一些东西。这段代码可以工作,但似乎比它需要的更“冗长”。似乎我见过人们做do..while的递归,但我不确定它在这里是否适用 resolve($array)应使用同一$array中的适当字符串替换任何“{%tags%}”,并在嵌套时重复该过程。尽可能简化-希望这有意义: <?php $strings = [ "title":"My Program", "first_string&quo
do..while
的递归,但我不确定它在这里是否适用
resolve($array)
应使用同一$array
中的适当字符串替换任何“{%tags%}”,并在嵌套时重复该过程。尽可能简化-希望这有意义:
<?php
$strings = [
"title":"My Program",
"first_string":"<b>{% title %}:</b> The second_string text is {% second_string %}",
"second_string"=>"Hello, World! Do you like {% title %}?"
];
function resolve($strings) {
$regex = "/{% ([^{}]*?) %}/"; //RegEx for getting {% tags %}
foreach($strings as $str_key=>$str_val) {
//if the string has any {% tags %}...
if(preg_match_all($regex,$str_val,$tmp_tags_arr)) {
list($tags,$tagNames) = $tmp_tags_arr;
foreach($tagNames as $tag_key=>$tag_name) //for every tagname...
$strings[$str_key] = str_replace(
$tags[$tag_key],
$strings[$tag_name],
$strings[$str_key]
); //replace the whole {% tag %} with appropriate value
}
}
/*****This part just seems overly redundant******/
//Rerun all the $strings to check if there are still unresolved {% tags %}
foreach($strings as $str_key=>$str_val)
if(preg_match_all($regex,$str_val,$tags))
$strings = resolve($strings);
return $strings;
}
var_dump(resolve($strings));
// Returns:
// array (size=3)
// 'title' => string 'My Program' (length=10)
// 'first_string' => string '<b>My Program:</b> The second_string text is Hello, World!' (length=58)
// 'second_string' => string 'Hello, World! Do you like My Program?' (length=13)
?>
我的问题是:做这样一个递归的更合适的方法是什么
编辑:
$strings['first_string']
引用了'title'
和'second_string'
,但'second_string'
本身包含一个{%tag%}。。。因此需要递归。无需递归,因为键必须是唯一的,我们知道它们是什么,只需循环这些键即可。由于str\u replace
接受数组,因此每次通过循环都可以替换整个数组:
foreach(array_keys($strings) as $tag) {
$strings = str_replace("{% $tag %}", $strings[$tag], $strings);
}
print_r($strings);
array\u keys($strings)
的值是'title','first\u string','second\u string'
,因此循环如下:
迭代1:在整个数组中搜索{%title%}
,并将其替换为$strings
$strings = [
"title"=>"My Program",
"first_string"=>"<b>My Program:</b> The second_string text is {% second_string %}",
"second_string"=>"Hello, World! Do you like My Program?"
];
$strings = [
"title"=>"My Program",
"first_string"=>"<b>My Program:</b> The second_string text is {% second_string %}",
"second_string"=>"Hello, World! Do you like My Program?"
];
$strings = [
"title"=>"My Program",
"first_string"=>"<b>My Program:</b> The second_string text is Hello, World! Do you like My Program?",
"second_string"=>"Hello, World! Do you like My Program?"
];
迭代3:在整个数组中搜索{%second\u string%}
,并将其替换为$strings
$strings = [
"title"=>"My Program",
"first_string"=>"<b>My Program:</b> The second_string text is {% second_string %}",
"second_string"=>"Hello, World! Do you like My Program?"
];
$strings = [
"title"=>"My Program",
"first_string"=>"<b>My Program:</b> The second_string text is {% second_string %}",
"second_string"=>"Hello, World! Do you like My Program?"
];
$strings = [
"title"=>"My Program",
"first_string"=>"<b>My Program:</b> The second_string text is Hello, World! Do you like My Program?",
"second_string"=>"Hello, World! Do you like My Program?"
];
$strings=[
“标题”=>“我的程序”,
“第一个字符串”=>“我的程序:第二个字符串文本是Hello,World!你喜欢我的程序吗?”,
“第二个字符串”=>“你好,世界!你喜欢我的程序吗?”
];
对不起,我已修复了该功能
resolve()
在其内部被调用。那么,$strings
的哪个值会导致递归发生?变量?像{%{%variable%}%}
?这毫无意义,是的$string['first\u string']
引用了'title'
和'second\u string'
<代码>'second_string'在第一次使用时不会被拾取,因此整个函数必须重新开始。这有意义吗?@MikeB的复制品你刚才声称这个问题是它本身的复制品。。。lol编辑:现在我明白了。在这种情况下(我已经更新了上面的$strings
数组),递归是必要的,因为没有,$strings['first\u string']
='my Program:第二个字符串文本是Hello,World!你喜欢{%title%}吗?'
。。。最后是{%title%}
。这太干净了,我喜欢它的甜。。。这比我做的要简单得多特沃斯。我不知道为什么会这样。。。为什么这又起作用了?@philtune如果你很难掌握它,只需通过迭代将其分解,并分析每次迭代中发生的事情。只要Abracadver同意@sjagr的解释,我已经做了一个编辑并解释了。谢谢