Rumah >pembangunan bahagian belakang >tutorial php >php 数组组合
$set = array('a','b','c','d');
该如何把数组组合,输出的结果是
a
b
c
b
ab
ac
ad
bc
bd
$set = array('a','b','c','d');$res = $set;for($i=0, $p=1; $i<count($res) && isset($set[$p]); $i++, $p++) { $res[] = $res[$i] . $set[$p];}print_r($res);Array
$set = array('a','b','c','d');$res = $set;for($i=0, $p=1; $i<count($res) && isset($set[$p]); $i++, $p++) { $res[] = $res[$i] . $set[$p];}print_r($res);Array
<?php$set = array('a','b','c','d');$len = count($set);$res = array();for($i=0,$k=$len;$i<$len;$i++){ $res[$i] = $set[$i]; for($j=$i+1;$j<$len;$j++){ $res[$k++] = $set[$i].$set[$j]; }}ksort($res);var_dump($res);?>
array (size=10) 0 => string 'a' (length=1) 1 => string 'b' (length=1) 2 => string 'c' (length=1) 3 => string 'd' (length=1) 4 => string 'ab' (length=2) 5 => string 'ac' (length=2) 6 => string 'ad' (length=2) 7 => string 'bc' (length=2) 8 => string 'bd' (length=2) 9 => string 'cd' (length=2)
$set = array('a','b','c','d');$arr=array();for($j=0;$j<sizeof($set);$j++){ $arr[]=$set[$j];}foreach($set as $k=>$v){ for($i=$k;$i<=(3-$v);$i++){ if($v==$set[$i]) continue; $arr[]=$v.$set[$i]; }}print_r($arr);
Array( [0] => a [1] => b [2] => c [3] => d [4] => ab [5] => ac [6] => ad [7] => bc [8] => bd [9] => cd)