我试图获取一个字符串并显示它的可能组合(在PHP中),但同时按每个单词的顺序说。例如:“你好吗”将返回(一个数组)
How are you How are are you how you are
我现在拥有的代码可以显示所有组合,但我希望它能使它们保持顺序,而不要乱动单词。任何人都有他们想分享的想法或摘要吗?谢谢
设置两个迭代器并打印它们之间的所有内容。所以像这样:
<? $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