Heim >Backend-Entwicklung >PHP-Tutorial >5组数字,每组取一个组成不相同的5位数,大神
例 : 012345678,013456789,023456789,012456789,023456789
这5组数每组取一个组成不相同的5位数
$d = explode(',', '012345678,013456789,023456789,012456789,023456789');$r = array();for($i=0; $i<count($d); $i++) { for($j=0; $j<strlen($d[$i]); $j++) if(! in_array($d[$i]{$j}, $r)) { $r[] = $d[$i]{$j}; break; }}echo join(' ', $r);你可能需要有随机性的
$d = explode(',', '012345678,013456789,023456789,012456789,023456789');$r = array();for($i=0; $i<count($d); $i++) { $loop = 100; do { $j = rand(0, strlen($d[$i]) - 1); if(! in_array($d[$i]{$j}, $r)) { $r[] = $d[$i]{$j}; break; } }while($loop--);}echo join(' ', $r);
我是想把所有能组成的5位数都显示出来,我没写清楚
循环 去重呗
$d = array_map('str_split', explode(',', '012345678,013456789,023456789,012456789,023456789'));print_r(foo($d));function foo($d) { $r = array_pop($d); while($d) { $t = array(); foreach(array_pop($d) as $k1=>$x) { foreach($r as $k2=>$y) $t[] = "$x $y"; } $r = $t; } return $r;}