방법 1:
<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><=<span>$m</span>; <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>
방법 2:
<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><<span>$level</span>;<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><<span>$num</span>;<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><<span>$len</span>){ <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 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.