php 重新组合数组_PHP数组组合

4161780dd482be4193f385a2f1faf27f.png

慕神8447489

您可以使用这里找到的解决方案http://stereofrog.com/blok/on/070910.万一链接掉了,这是代码.。class Combinations implements Iterator{

    protected $c = null;

    protected $s = null;

    protected $n = 0;

    protected $k = 0;

    protected $pos = 0;

    function __construct($s, $k) {

        if(is_array($s)) {

            $this->s = array_values($s);

            $this->n = count($this->s);

        } else {

            $this->s = (string) $s;

            $this->n = strlen($this->s);

        }

        $this->k = $k;

        $this->rewind();

    }

    function key() {

        return $this->pos;

    }

    function current() {

        $r = array();

        for($i = 0; $i k; $i++)

            $r[] = $this->s[$this->c[$i]];

        return is_array($this->s) ? $r : implode('', $r);

    }

    function next() {

        if($this->_next())

            $this->pos++;

        else

            $this->pos = -1;

    }

    function rewind() {

        $this->c = range(0, $this->k);

        $this->pos = 0;

    }

    function valid() {

        return $this->pos >= 0;

    }

    protected function _next() {

        $i = $this->k - 1;

        while ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i)

            $i--;

        if($i 

            return false;

        $this->c[$i]++;

        while($i++ k - 1)

            $this->c[$i] = $this->c[$i - 1] + 1;

        return true;

    }}foreach(new Combinations("1234567", 5) as $substring)

    echo $substring, ' ';12345 12346 12347 12356 12357 12367 12456 12457 12467 12567 13456 13457 13467 13567 14567 23456 23457 23467 23567 24567 34567

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值