<?php
function foo($len=4) {
$s = chunk_split('012345', 1);
$ar = split("[\r\n]+", $s);
array_pop($ar);
shuffle($ar);
return join('', array_slice($ar, rand(0, count($ar) - $len), $len));
}
echo foo().'<br>';
echo foo().'<br>'; 这是唠叨老大给我的,我想你应该能用上
echo foo(4).'<br>';
echo foo(4).'<br>';
?>
function foo($len=4) {
$s = chunk_split('012345', 1);
$ar = split("[\r\n]+", $s);
array_pop($ar);
shuffle($ar);
return join('', array_slice($ar, rand(0, count($ar) - $len), $len));
}
echo foo().'<br>';
echo foo().'<br>'; 这是唠叨老大给我的,我想你应该能用上
echo foo(4).'<br>';
echo foo(4).'<br>';
?>
static $ret = array();
if($m == 0) {
$m = count($ar);
$ret = array();
}
for($i=$m; $i>=$k; $i--) {
$a[$k-1] = $ar[$i-1];
if($k > 1) {
combination(&$ar, $k-1, $i-1, $a);
}else {
array_unshift ($ret, array_reverse($a));
}
}
return $ret;
}print_r(combination(array(0,1,2,3,4,5), 4));
{
判断$i的几个数字是不是012345
}
for ($i=1023; $i<=5432; $i++)
{
判断$i的几个数字是不是012345
}
/**
* @类名: Permutation
* @功能:求从m个数中取任取n个数的的排列和组合
* @作者:johnpanq
* @创建:2006.2.23
* @更新:2007.1.26
* @示例:
* $m = array(1,2,3);
* $num = new Permutation($m);
* getList($n,$order=1) $n为取得个数,$order=1为排列,$order=2为组合
* 如取两个数的排列
* $arr = $num->getList(2);
* 如取两个数的组合
* $arr = $num->getList(2,2);
* $arr排列组合的二维数组
*/
class Permutation
{
var $srcList; //存放原始数列数组
var $rstList; //存放最终数列数组
var $srcLen; //原始数列的长度
var $rstLen; //结果数列的长度
var $rstOrder; //求排列或组合标志 function Permutation($m)
{
if (!is_array($m))
{
echo '参数类型错误:Permutation(array $m) $m为数组类型';
return false;
}
$this->srcLen = count($m);
$this->srcList = $m;
$this->optimize = false;
}
//取得排列组合结果, $order=1为取排列,$order=2为取组合
function getList($n, $order=1)
{
if (!is_int($n))
{
echo '参数类型错误:getList(int $n) $n为整数类型';
return false;
}
if ($n > $this->srcLen)
{
echo '要求的排列组合的长度不能大于原始数列的长度';
return false;
}
$this->rstLen = $n;
$this->rstOrder = $order;
$this->rstList = array();
$this->doPermutation($this->srcList, $this->rstLen);
return $this->rstList;
}
//递归计算从m中取n的排列, $rst存取出的排列
function doPermutation($m, $n, &$rst=array())
{
//如果已经取完
if ($n == 0)
{
$this->rstList[] = $rst;
$rst = array();
return true;
}
//从当前数列m中选取一个值,并更新数列m
foreach($m as $key=>$value)
{
$nextM = $m;
//判断是取排列还是组合
switch($this->rstOrder)
{
//取排列, 清空当前取出的值
case 1: unset($nextM[$key]); break;
//取组合, 清空当前及前面所取的值
case 2: array_splice($nextM,0,$key+1); break;
default:unset($nextM[$key]); break;
}
$nextRst = $rst;
//将取的值放到结果数列中
$nextRst[] = $value;
$this->doPermutation($nextM,$n-1,$nextRst);
}
}
}$m = array(0,1,2,3,4,5);
$num = new Permutation($m);
$arr = $num->getList(4);
foreach($arr as $value)
{
echo implode(",",$value)."<br>";
}
?>