3

I thought my problem had been solved using this solution, using the answer by VolkerK, but it doesn't seem to be working correctly.

What I want is a function that returns all possible combinations of values contained in nested arrays.

For example, if I pass in

[ ['a', 'b'], ['a', 'b'], ['a', 'b'], ['a'], ['a'], ['a'] ]

It would return

a, a, a, a, a, a
b, b, b, a, a, a
a, a, b, a, a, a
a, b, a, a, a, a
b, a, a, a, a, a
a, b, b, a, a, a
b, b, a, a, a, a
b, a, b, a, a, a

The problem with using VolkerK's answer as below, is that it is just returning

a, a, a, a, a, a
b, b, b, a, a, a
a, a, a, a, a, a
b, b, b, a, a, a
a, a, a, a, a, a
b, b, b, a, a, a
a, a, a, a, a, a
b, b, b, a, a, a

How can the below code be fixed to return the proper combination I made above? (or can you write a new function that does the above?)

<?php
class PermArray implements  ArrayAccess {
    // todo: constraints and error handling - it's just an example
    protected $source;
    protected $size;

    public function __construct($source) {
        $this->source = $source;
        $this->size = 1;
        foreach ( $source as $a ) {
            $this->size *= count($a);
        }
    }
    public function count() { return $this->size; }

    public function offsetExists($offset) { return is_int($offset) && $offset < $this->size; }
    public function offsetGet($offset) {
        $rv = array();
        for ($c = 0; $c < count($this->source); $c++) {
          $index = ($offset + $this->size) % count($this->source[$c]);
          $rv[] = $this->source[$c][$index];
        }
        return $rv;
    }

    public function offsetSet($offset, $value ){}
    public function offsetUnset($offset){}
}

$pa = new PermArray( [['x'], ['y', 'z', 'w'], ['m', 'n']] );
$cnt = $pa->count();
for($i=0; $i<$cnt; $i++) {
    echo join(', ', $pa[$i]), "\n";
}
4

2 回答 2

2

这是一个非常“直接”、不优雅(或者如果你愿意的话)的解决方案,并且不符合你的预期顺序(如果你在乎的话):

function P(array $sources)
{
    $result=array();
    $cache=array();
    foreach($sources as $node)
    {
        $cache=$result;
        $result=array();
        foreach($node as $item)
        {
            if(empty($cache))
            {
                $result[]=array($item);
            }
            else
            {
                foreach($cache as $line)
                {
                    $line[]=$item;
                    $result[]=$line;
                }
            }
        }
    }
    return $result;
}
$result=P(array(array('a','b'),array('a','b'),array('a','b'),array('a'),array('a'),array('a')));
print_r(array_map(function($a){return implode(",",$a);},$result));

现场演示

输出:

Array
(
    [0] => a,a,a,a,a,a
    [1] => b,a,a,a,a,a
    [2] => a,b,a,a,a,a
    [3] => b,b,a,a,a,a
    [4] => a,a,b,a,a,a
    [5] => b,a,b,a,a,a
    [6] => a,b,b,a,a,a
    [7] => b,b,b,a,a,a
)

我更改了您的[]语法以array()提供更多向后兼容(但匿名函数需要 PHP 5.3)。

于 2013-11-08T08:52:14.377 回答
0

我在 JavaScript 中有一个可以做到这一点,但它可能会被移植到 PHP。

https://github.com/adamjgrant/permutations

于 2020-05-28T20:34:02.130 回答