我试图获取一个字符串并显示它的可能组合(在PHP中),但同时按每个单词的顺序说.例如:你好吗"将返回(一个数组)
I am trying to take a string and display the possible combinations of it (in PHP), but while saying in order of each word. For example: "how are you" would return (an array)
How are you How are are you how you are我现在拥有的代码可以显示所有组合,但是我希望它能使它们保持顺序,而不是乱七八糟.任何人都有他们想分享的想法或摘要吗?谢谢
The code I have now displays all combinations but I am wanting it to keep them in order and not flip words. Any one have any ideas or snippets they care to share? Thanks
推荐答案设置两个迭代器,并在它们之间打印所有内容.像这样:
Set two iterators and print everything between them. So something like this:
<? $str = "How are you"; $words = explode(" ",$str); $num_words = count($words); for ($i = 0; $i < $num_words; $i++) { for ($j = $i; $j < $num_words; $j++) { for ($k = $i; $k <= $j; $k++) { print $words[$k] . " "; } print "\n"; } } ?>输出
How How are How are you are are you you