php – 如何返回给定字符串的所有组合? (例如’foo bar’= bar,bar_foo,foo)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 如何返回给定字符串的所有组合? (例如’foo bar’= bar,bar_foo,foo),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3106字,纯文字阅读大概需要5分钟。
内容图文
![php – 如何返回给定字符串的所有组合? (例如’foo bar’= bar,bar_foo,foo)](/upload/InfoBanner/zyjiaocheng/725/a679a18ba59d4cba8a0d8de3386477ef.jpg)
此问题与上述建议的问题不重复.标题可能听起来相似,但其答案绝不会导致下面问题中描述的结果.
我很难递归地迭代未知长度的数组来创建独特的字符串组合.你能帮我吗?
目标是采用像foo bar这样的字符串,并从该字符串创建唯一的组合:
foo
bar
bar_foo (alphabetized to make unique combinations, not permutations)
另一个例子:
车栏添加应返回:
add
add_bar
add_car
add_bar_car
bar
bar_car
car
这是我的进步:
function string_builder($length) {
$arrWords = array('add','bar','car','dew','eat','fat','gym','hey','ink','jet','key','log','mad','nap','odd','pal','qat','ram','saw','tan','urn','vet','wed','xis','yap','zoo');
$arr = array();
for ($i=0; $i < $length; $i++) {
$arr[] = $arrWords[$i];
}
return implode(' ', $arr);
}
function get_combinations($string) {
$combinations = array(); // put all combinations here
$arr = explode(' ',$string);
$arr = array_unique($arr); // only unique words are important
sort($arr); // alphabetize to make unique combinations easier (not permutations)
$arr = array_values($arr); // reset keys
for ($i=0; $i < count($arr); $i++) {
// this is where I'm stuck
// how do I loop recursively through all possible combinations of an array?
}
return $combinations;
}
// Test it!
for ($i=1; $i < 26; $i++) {
$string = string_builder($i);
$combinations = get_combinations($string);
echo $i . " words\t" . count($combinations) . " combinations\t" . $string . "\n";
// print_r($combinations);
}
另一种尝试:
function getCombinations2($str, $min_length = 2) {
$words = explode(' ', $str);
$combinations = array();
$len = count($words);
for ($a = $min_length; $a <= $min_length; $a++) {
for ($pos = 0; $pos < $len; $pos ++) {
if(($pos + $a -1) < $len) {
$tmp = array_slice($words, $pos, $a);
sort($tmp);
$tmp = implode('_',$tmp);
$combinations[] = $tmp;
}
}
}
$combinations = array_unique($combinations);
return $combinations;
}
当您打印出组合并寻找应该存在的几个组合时,您可以知道您是成功的(例如,“fat_zoo”,“car_tan”).我的两次尝试都会展示其中的几个,但绝不是全部.
解决方法:
可能有一个更优雅的解决方案,但我用2个功能做到了.
getCombosOfLength函数为数组中的每个$intLength组合. GetCombos函数只为您想要的每个长度运行GetCombosOfLength.这非常适合生成1-5项的所有组合.如果你为所有25项组合运行它,它有一些问题.
$a = array("c", "b", "f", "v", "g", "e", "h", "i", "j", "k", "l", "m", "n", "p", "o", "r", "a", "q", "s", "t", "u", "w", "x", "y", "z");
$b = getCombos($a, 5);
print "<pre>\n";
print_r($b);
function getCombos($arrInput, $intMax = null) {
sort($arrInput);
if (is_null($intMax)) $intMax = count($arrInput);
$arrOutput = array();
for ($i = $intMax; $i > 0; $i--) {
$arrReturn = getCombosOfLength($arrInput, $i);
for ($j = 0; $j < count($arrReturn); $j++) $arrOutput[] = $arrReturn[$j];
}
return $arrOutput;
}
function getCombosOfLength($arrInput, $intLength) {
$arrOutput = array();
if ($intLength == 1) {
for ($i = 0; $i < count($arrInput); $i++) $arrOutput[] = array($arrInput[$i]);
return $arrOutput;
}
$arrShift = $arrInput;
while (count($arrShift)) {
$x = array_shift($arrShift);
$arrReturn = getCombosOfLength($arrShift, $intLength - 1);
for ($i = 0; $i < count($arrReturn); $i++) {
array_unshift($arrReturn[$i], $x);
$arrOutput[] = $arrReturn[$i];
}
}
return $arrOutput;
}
内容总结
以上是互联网集市为您收集整理的php – 如何返回给定字符串的所有组合? (例如’foo bar’= bar,bar_foo,foo)全部内容,希望文章能够帮你解决php – 如何返回给定字符串的所有组合? (例如’foo bar’= bar,bar_foo,foo)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。