PHP采取一切组合 [英] PHP take all combinations

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

问题描述

我看到这个<一href="http://stackoverflow.com/questions/1256117/algorithm-that-will-take-numbers-or-words-and-find-all-possible-combinations">algorithm将采取数字或单词,并找到所有可能的组合

和我使用它,但它不会返回所有的真实的组合。

PHP:

 &LT; PHP
    require_once'数学/ Combinatorics.php;
    $字=阵列(猫,狗,鱼);
    $组合数学=新Math_Combinatorics;
    的foreach($ combinatorics-&GT;置换($字,2)$ P){
        回声加入('',$ P),\ N的;
    }
?&GT;
 

和它返回:

 猫狗
狗猫
鲶鱼
鱼的猫
狗鱼
鱼狗
 

但这些都不是真正的所有组合,所有真正的组合,包括这些也:

 猫猫
狗狗
鱼鱼
 

而这正是我需要的,该方法以获得所有真正的组合:

 猫狗
狗猫
鲶鱼
鱼的猫
狗鱼
鱼狗
猫猫
狗狗
鱼鱼
 

解决方案

好了,这里是你的code(和BTW,感谢张贴这样一个有趣和富有挑战性的问题 - 至少对我来说... :-)) - 使用递归作为给定元素的数组所有可能的排列(由N))

code:

 &LT; PHP

功能置换($改编,$ N)
{
     $水库=阵列();

     的foreach($改编为$ W)
     {
           如果($ N == 1)$水库[] = $ W;
           其他
           {
                 $烫发=排列($改编,$ N-1);

                 的foreach($烫发为$ P)
                 {
                      $水库[] = $ W$页。
                 }
           }
     }

     返回$水库;
}

//你的数组
$字=阵列(猫,狗,鱼);

//由3个元素组获取置换
$ PE =置换($话,3);

//打印出来
的print_r($ PE);

?&GT;
 

输出:

 阵列
(
    [0] =&GT;猫猫猫
    [1] =&GT;猫猫狗狗
    [2] =&GT;猫猫鱼
    [3] =&GT;猫的狗猫
    [4] =&GT;猫狗狗
    [5] =&GT;猫狗鱼
    [6] =&GT;猫吃鱼的猫
    [7] =&GT;猫吃鱼狗
    [8] =&GT;猫吃鱼的鱼
    [9] =&GT;狗猫的猫
    [10] =&GT;狗猫狗
    [11] =&GT;狗猫鱼
    [12] =&GT;狗狗猫
    [13] =&GT;狗狗狗
    [14] =&GT;狗狗鱼
    [15] =&GT;狗鱼的猫
    [16] =&GT;狗鱼狗
    [17] =&GT;狗鱼的鱼
    [18] =&GT;鱼的猫猫
    [19] =&GT;猫鱼狗
    [20] =&GT;鱼猫吃鱼
    [21] =&GT;鱼狗猫
    [22] =&GT;鱼狗狗
    [23] =&GT;鱼狗鱼
    [24] =&GT;鱼鱼的猫
    [25] =&GT;鱼吃鱼狗
    [26] =&GT;鱼鱼鱼
)
 


提示:排列($字,2),你就可以得到你想要的到底是什么...

I saw this algorithm that will take numbers or words and find all possible combinations

And I'm using it, but it does NOT return all "real" combinations.

PHP:

<?php
    require_once 'Math/Combinatorics.php';
    $words = array('cat', 'dog', 'fish');
    $combinatorics = new Math_Combinatorics;
    foreach($combinatorics->permutations($words, 2) as $p) {
        echo join(' ', $p), "\n"; 
    }
?>

And it returns:

cat dog
dog cat
cat fish
fish cat
dog fish
fish dog

But these are not all real combinations, all real combinations includes these too:

cat cat
dog dog
fish fish

And that is what I need, the method to get all real combinations:

cat dog
dog cat
cat fish
fish cat
dog fish
fish dog
cat cat
dog dog
fish fish

解决方案

OK, here's your code (and btw, thanks for posting such an interesting and challenging problem - at least for me... :-)) - using recursion for all possible permutations (by N) given an array of elements)

Code :

<?php

function permutations($arr,$n)
{
     $res = array();

     foreach ($arr as $w)
     {
           if ($n==1) $res[] = $w;
           else
           {
                 $perms = permutations($arr,$n-1);

                 foreach ($perms as $p)
                 {
                      $res[] = $w." ".$p;
                 } 
           }
     }

     return $res;
}

// Your array
$words = array('cat','dog','fish');

// Get permutation by groups of 3 elements
$pe = permutations($words,3);

// Print it out
print_r($pe);

?>

Output :

Array
(
    [0] => cat cat cat
    [1] => cat cat dog
    [2] => cat cat fish
    [3] => cat dog cat
    [4] => cat dog dog
    [5] => cat dog fish
    [6] => cat fish cat
    [7] => cat fish dog
    [8] => cat fish fish
    [9] => dog cat cat
    [10] => dog cat dog
    [11] => dog cat fish
    [12] => dog dog cat
    [13] => dog dog dog
    [14] => dog dog fish
    [15] => dog fish cat
    [16] => dog fish dog
    [17] => dog fish fish
    [18] => fish cat cat
    [19] => fish cat dog
    [20] => fish cat fish
    [21] => fish dog cat
    [22] => fish dog dog
    [23] => fish dog fish
    [24] => fish fish cat
    [25] => fish fish dog
    [26] => fish fish fish
)


HINT : By permutations($words,2), you'll be able to get exactly what you wanted...

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

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