本文實例講述了PHP實現(xiàn)給定一列字符,生成指定長度的所有可能組合。分享給大家供大家參考,具體如下:
給定一列字符,生成指定長度的所有可能的組合:
如:a,b,c,d,e 或 0-9
生成長度 1:a, b, c, d, e; 長度2 :aa, ab, ac, ad, ae, ba, bb, bc, bd, be,................ee
?php function de($len, $pos = 0) { static $bit = []; static $source = ['a', 'b', 'c', 'd', 'e'];/*[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];*/ $pos++; for($i = 0; $i count($source); $i++) { $bit[$pos] = $source[$i]; if ($pos $len) { de($len, $pos); } else { echo implode('', $bit)."\n"; } } }
用phpcmd小助手(https://github.com/dclnet/phpcmd)運行代碼
以上為長度為1
長度為2的。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)學運算技巧總結(jié)》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》及《php正則表達式用法總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。