PHP 配列の組み合わせ
$set = array('a','b','c','d');
配列の結合方法、出力結果は
a
b
c
b
ab
ac
ad
bc
bd
------解決策------- - --------------
$set = array('a','b','c','d');<br />$res = $set;<br />for($i=0, $p=1; $i<count($res) && isset($set[$p]); $i++, $p++) {<br /> $res[] = $res[$i] . $set[$p];<br />}<br />print_r($res);
<?php<br />$set = array('a','b','c','d');<br />$len = count($set);<br />$res = array();<br />for($i=0,$k=$len;$i<$len;$i++){<br /> $res[$i] = $set[$i];<br /> for($j=$i+1;$j<$len;$j++){<br /> $res[$k++] = $set[$i].$set[$j];<br /> }<br />}<br />ksort($res);<br />var_dump($res);<br />?>
array (size=10)<br /> 0 => string 'a' (length=1)<br /> 1 => string 'b' (length=1)<br /> 2 => string 'c' (length=1)<br /> 3 => string 'd' (length=1)<br /> 4 => string 'ab' (length=2)<br /> 5 => string 'ac' (length=2)<br /> 6 => string 'ad' (length=2)<br /> 7 => string 'bc' (length=2)<br /> 8 => string 'bd' (length=2)<br /> 9 => string 'cd' (length=2)<br />