PHP nested array combinations / permutations

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 02:33:29

问题


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";
}

回答1:


Here is a quite "straight", un-elegant (or ugly if you will) solution, and didn't match your expected order (if you care):

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));

Live demo

Output:

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
)

I changed your [] syntax to array() to provide more backward compatible (but anonymous function requires PHP 5.3).



来源:https://stackoverflow.com/questions/19853989/php-nested-array-combinations-permutations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!