方法一:
<code><span>$str</span> = <span>'abc'</span>; <span>$a</span> =str_split(<span>$str</span>); perm(<span>$a</span>, <span>0</span>, count(<span>$a</span>)-<span>1</span>); <span><span>function</span><span>perm</span><span>(&<span>$ar</span>, <span>$k</span>, <span>$m</span>)</span> {</span><span>if</span>(<span>$k</span> == <span>$m</span>){ <span>echo</span> join(<span>''</span>,<span>$ar</span>), PHP_EOL; }<span>else</span> { <span>for</span>(<span>$i</span>=<span>$k</span>; <span>$i</span>$m; <span>$i</span>++) { swap(<span>$ar</span>[<span>$k</span>], <span>$ar</span>[<span>$i</span>]); perm(<span>$ar</span>, <span>$k</span>+<span>1</span>, <span>$m</span>); swap(<span>$ar</span>[<span>$k</span>], <span>$ar</span>[<span>$i</span>]); } } } <span><span>function</span><span>swap</span><span>(&<span>$a</span>, &<span>$b</span>)</span> {</span><span>$c</span> = <span>$a</span>; <span>$a</span> = <span>$b</span>; <span>$b</span> = <span>$c</span>; }</code>
方法二:
<code><span><span>function</span><span>output</span><span>(<span>$temp</span>,<span>$level</span>)</span> {</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$level;<span>$i</span>++) { <span>echo</span><span>$temp</span>[<span>$i</span>]; } <span>echo</span> PHP_EOL; } <span>//产生全排列 递归参数传递一定要注意 </span><span><span>function</span><span>quanpai</span><span>(<span>$arr</span>,<span>$flag</span>,<span>$level</span>,<span>$num</span>,<span>$temp</span>)</span> {</span><span>if</span>(<span>$level</span>>=<span>$num</span>) { output(<span>$temp</span>,<span>$num</span>); <span>return</span>; } <span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$num;<span>$i</span>++) { <span>if</span>(<span>$flag</span>[<span>$i</span>]==<span>0</span>) { <span>$temp</span>[<span>$level</span>]=<span>$arr</span>[<span>$i</span>]; <span>$flag</span>[<span>$i</span>]=<span>1</span>; quanpai(<span>$arr</span>,<span>$flag</span>,<span>$level</span>+<span>1</span>,<span>$num</span>,<span>$temp</span>); <span>$flag</span>[<span>$i</span>]=<span>0</span>; } } } <span>$str</span> = <span>'abc'</span>; <span>$arr</span>=str_split(<span>$str</span>); <span>$len</span>=count(<span>$arr</span>); <span>$i</span>=<span>0</span>; <span>$flag</span>=<span>array</span>(); <span>$temp</span>=<span>array</span>(); <span>while</span>(<span>$i</span>$len){ <span>$flag</span>[<span>$i</span>]=<span>0</span>; <span>$i</span>++; } quanpai(<span>$arr</span>,<span>$flag</span>,<span>0</span>,<span>$len</span>,<span>$temp</span>);</code>
以上就介绍了PHP字符串全排列,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。