PHP数组组合 [英] PHP array combinations

查看:50
本文介绍了PHP数组组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 7 个数字(1、2、3、4、5、6、7)组成的数组,我想选择其中的 5 个数字,例如
(1,2,3,4,5), (1,2,3,4,6), (1,2,3,4,7).
请注意,(1,2,3,4,5) 等于 (4,5,3,1,2),因此输出中只应包含其中之一.

我想知道 PHP 中是否有函数或任何算法可以做到这一点?我不知道从哪里开始.你能帮我吗?

我希望将 7 个给定数字的所有组合(它们取自一个数组)放入 5 个槽中,不考虑顺序.

解决方案

您可以使用此处找到的解决方案 http://stereofrog.com/blok/on/070910.

如果链接失效,这里是代码....

class Combinations 实现 Iterator{受保护的 $c = 空;受保护的 $s = null;保护 $n = 0;受保护的 $k = 0;受保护的 $pos = 0;函数 __construct($s, $k) {if(is_array($s)) {$this->s = array_values($s);$this->n = count($this->s);} 别的 {$this->s = (string) $s;$this->n = strlen($this->s);}$this->k = $k;$this->rewind();}功能键(){返回 $this->pos;}函数电流(){$r = 数组();for($i = 0; $i <$this->k; $i++)$r[] = $this->s[$this->c[$i]];返回 is_array($this->s) ?$r : 内爆('', $r);}功能下一个(){if($this->_next())$this->pos++;别的$this->pos = -1;}功能倒带(){$this->c = range(0, $this->k);$this->pos = 0;}功能有效(){返回 $this->pos >= 0;}受保护的函数 _next() {$i = $this->k - 1;而 ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i)$i--;如果($i <0)返回假;$this->c[$i]++;while($i++ < $this->k - 1)$this->c[$i] = $this->c[$i - 1] + 1;返回真;}}foreach(new Combinations("1234567", 5) as $substring)回声 $substring, ' ';

12345 12346 12347 12356 12357 12367 12456 12457 12467 12567 13456 13457 13467 13567 14567 23467 23457 23457 236 5 7 5 5 7 5

I have an array of 7 numbers (1,2,3,4,5,6,7) and I want to choose 5 of the numbers like
(1,2,3,4,5), (1,2,3,4,6), (1,2,3,4,7).
Note that (1,2,3,4,5) is equal to (4,5,3,1,2), so only one of those should be included in the output.

I would like to know if there is a function in PHP or any algorithm that can do this ? I have no idea where to start from. Can you help me ?

I want all the combinations of 7 given numbers ( they are taken from an array ) put into 5 slots, disregarding order.

解决方案

You can use the solution found here http://stereofrog.com/blok/on/070910.

Incase the link goes down here's the code....

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 < $this->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 < 0)
            return false;
        $this->c[$i]++;
        while($i++ < $this->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

这篇关于PHP数组组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆