我试图取一个字符串并显示它的可能组合(在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