PHP嵌套数组组合/排列 [英] PHP nested array combinations / permutations

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

问题描述

我想我的问题一直在使用这个解决方案解决,通过使用VolkerK答案,但它似乎并没有正常工作。

我要的是返回包含在嵌套数组值的所有可能组合的功能。

例如,如果我通过在

['A','B'],['一','B'],['A','B'],['一'],['一' ],['一']

这将返回

  A,A,A,A,A,A
B,B,B,A,A,A
一,A,B,一,一,一
的a,b,一,一,一,一
B,A,A,A,A,A
A,B,B,A,A,A
B,B,A,A,A,A
B,A,B,A,A,A

用如下VolkerK的回答的问题是,它只是返回

  A,A,A,A,A,A
B,B,B,A,A,A
一,一,一,一,一,一
B,B,B,A,A,A
一,一,一,一,一,一
B,B,B,A,A,A
一,一,一,一,一,一
B,B,B,A,A,A

如何能在低于code被固定为返回我上面所做的正确的组合? (或者你可以写一个新的函数,它上面?)

 < PHP
类PermArray实现了ArrayAccess {
    // TODO:约束和错误处理 - 它只是一个例子
    保护$来源;
    保护$大小;    公共职能__construct($源){
        $这个 - >源= $来源;
        $这个 - >大小= 1;
        的foreach($源为$ a){
            $这个 - >大小* =计数($ A);
        }
    }
    公共函数count(){$返回这 - >大小; }    公共职能offsetExists($偏移){返回is_int($偏移)及与放大器; $偏移LT; $这个 - >大小; }
    公共职能offsetGet($偏移){
        $ RV =阵列();
        为($ C = 0; $ C<计数($这个 - >源); $ C ++){
          $指数=($抵消+ $这个 - >大小)计数%($这个 - >源[$ C]);
          $ RV [] = $这个 - >源[$ C] [$指数]
        }
        返回$ RV;
    }    公共职能offsetSet($抵消,$值){}
    公共职能offsetUnset($偏移){}
}$ PA =新PermArray(['X'],['Y','Z','W'],['M','N']);
$ CNT = $纸张>计数();
为($ I = 0; $ I< $ CNT; $ I ++){
    呼应加入('',每年$ [$ i]),\\ n;
}


解决方案

下面是一个很直,未优雅(或者如果你愿意丑)解决方案,并没有您的预计顺序一致(如果你关心)

 函数P(数组$源)
{
    $结果=阵列();
    $缓存阵列=();
    的foreach($来源为$节点)
    {
        $缓存= $结果;
        $结果=阵列();
        的foreach($节点作为$项)
        {
            如果(空($缓存))
            {
                $结果[] =数组($项目);
            }
            其他
            {
                的foreach($缓存$线)
                {
                    $行[] = $项目;
                    $结果[] = $行;
                }
            }
        }
    }
    返回$结果;
}
$结果= P(阵列(阵列('A','B'),阵列('A','B'),阵列('A','B'),阵列('A'),阵列('一个'),阵列('A')));
的print_r(array_map(函数($ A){返回破灭(,$ A);} $结果));

现场演示

输出:

 阵列

    [0] =>一,一,一,一,一,一
    [1] => B,A,A,A,A,A
    [2] =>的a,b,一,一,一,一
    [3] => B,B,A,A,A,A
    [4] =>一,A,B,一,一,一
    [5] => B,A,B,A,A,A
    [6] => A,B,B,A,A,A
    [7] => B,B,B,A,A,A

我改变了你的 [] 语法阵列()来提供更多的向后兼容(但匿名函数需要PHP 5.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";
}

解决方案

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

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

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