【问题标题】:PHP List All Combination of Elements of ArrayPHP列出数组元素的所有组合
【发布时间】:2014-12-06 15:51:34
【问题描述】:

我有一个数组:

$arr=array("A","B","C");

我想将其全部组合为:

array("A")
array("B")
array("C")
array("A","B")
array("A","C")
array("B","C")
array("A","B","C")

我想对所有这些组合进行处理,但我不想生成所有组合,将它们存储在数组中并将函数应用于它们。因为这需要大量内存和大组合。这个过程我有40个项目(我有很长的时间但我没有足够的内存)。

我想要这样的功能:

function ProcessArrayCombinations($array){
foreach($array as $v){
//generate and process next combination of array
print_r($nextcombination);
}
}

谢谢。

【问题讨论】:

    标签: php arrays process combinations


    【解决方案1】:

    此代码将组合识别为二进制数,使用的事实是有一个 formula,它指出 n 个元素的所有可能组合的总和是 2^n。知道它的二进制对数是整数,我们可以定义一个模型,其中从 n 位构造的每个可能的二进制数都是一组组合。代码未经测试,如有错别字,请在cmets中告诉我。

    function ProcessArrayCombinations($array) {
        $status = array();
        foreach ($array as $element) {
            $status[] = false;
        }
    
        $elementCount = count($status);
        $trues = 0;
    
        while ($trues < $elementCount) {
            $index = 0;
            $stop = false;
            while ((!$stop) && ($index < count($status)) && ($status[$index])) {
                $status[$index] = false;
                $trues--;
                $index++;
            }
            $status[$index] = true;
            $trues++;
            //Found a new combination
            //We should print elements from $array located at indexes fulfilling
            //the criteria that the element having the same index in $status is true:
            //for ($i = 0; $i < count($status); $i++) {
            //    if ($status[$i}) {
            //        print
            //    } else {
            //        don't print
            //    }
            //}
        }
    }
    

    【讨论】:

    • 感谢您的回复,但我不明白如何打印已建立的组合:/
    • 我找到了;谢谢你。这解决了我的要求。但是您可以将 $status.length 更改为 count($status) i++ 到 $i++ 和 $status.length 再次更改为 count($status)。
    • 没错,代码未经测试。我已经添加了你提出的修复。
    • 你可以改变 $elementCount = $status.length;也是。
    • 等等;我有一个新问题:我可以将起始元素设置为另一个吗?
    【解决方案2】:

    我编辑并使用了您的功能,如下所示。再次感谢 Lajos。

    function ProcessArrayCombinations($array) {
        $status = array();
        foreach ($array as $element) {
            $status[] = false;
        }
    
        $elementCount = count($status);
        $trues = 0;
    
        while ($trues < $elementCount) {
            $index = 0;
            $stop = false;
            while ((!$stop) && ($index < count($status)) && ($status[$index])) {
                $status[$index] = false;
                $trues--;
                $index++;
            }
            $status[$index] = true;
            $trues++;
            //Found a new combination
            //We should print elements from $array located at indexes fulfilling
            //the criteria that the element having the same index in $status is true:
            for ($i = 0; $i < count($status); $i++) {
                if ($status[$i]) {
                    echo $array[$i];
                }
            }
    echo '<br/>';
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-26
      • 2023-03-02
      • 1970-01-01
      • 2022-01-25
      • 2014-05-05
      • 2023-04-10
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多