【问题标题】:combination of array data [duplicate]数组数据的组合[重复]
【发布时间】:2013-03-25 05:47:30
【问题描述】:

有什么可以帮助我得到如下输出:

a:b 一个: 广告 一个:e 乙:丙 乙:d 是 c: d c: e d: e 甲:乙:丙 甲:乙:丁 甲:乙:乙 一个:c:d 甲:丙:乙 一个:d:e 乙:丙:丁 乙:丙:乙 乙:丁:乙 c:d:e A B C D 甲:乙:丙:乙 甲:乙:丁:乙 甲:丙:丁:乙 乙:丙:丁:乙 a: b: c: d: e

数据数组 $n = array('a', 'b', 'c', 'd', 'e');

我试过这样的代码:

for($a=0;$a<count($n);$a++)
{
    for($b=$a+1;$b<count($n);$b++)
    {
        echo $n[$a].' : '.$n[$b].'<br />';  
    }
}

for($a=0;$a<count($n);$a++)
{
    for($b=$a+1;$b<count($n);$b++)
    {
        for($c=$b+1;$c<count($n);$c++)
        {
            echo $n[$a].' : '.$n[$b].' : '.$n[$c].'<br />';     
        }       
    }
}

for($a=0;$a<count($n);$a++)
{
    for($b=$a+1;$b<count($n);$b++)
    {
        for($c=$b+1;$c<count($n);$c++)
        {
            for($d=$c+1;$d<count($n);$d++)
            {
                echo $n[$a].' : '.$n[$b].' : '.$n[$c].' : '.$n[$d].'<br />';            
            }           
        }       
    }
}

for($a=0;$a<count($n);$a++)
{
    for($b=$a+1;$b<count($n);$b++)
    {
        for($c=$b+1;$c<count($n);$c++)
        {
            for($d=$c+1;$d<count($n);$d++)
            {
                for($e=$d+1;$e<count($n);$e++)
                {
                    echo $n[$a].' : '.$n[$b].' : '.$n[$c].' : '.$n[$d].' : '.$n[$e].'<br />';               
                }               
            }           
        }       
    }
}

但是我觉得代码太长了,请帮忙简化一下,

谢谢

【问题讨论】:

  • 使用递归函数。

标签: php algorithm


【解决方案1】:

试试这个:

<?php
function getCombinations($base,$n){

$baselen = count($base);
if($baselen == 0){
    return;
}
    if($n == 1){
        $return = array();
        foreach($base as $b){
            $return[] = array($b);
        }
        return $return;
    }else{
        //get one level lower combinations
        $oneLevelLower = getCombinations($base,$n-1);

        //for every one level lower combinations add one element to them that the last element of a combination is preceeded by the element which follows it in base array if there is none, does not add
        $newCombs = array();

        foreach($oneLevelLower as $oll){

            $lastEl = $oll[$n-2];
            $found = false;
            foreach($base as  $key => $b){
                if($b == $lastEl){
                    $found = true;
                    continue;
                    //last element found

                }
                if($found == true){
                        //add to combinations with last element
                        if($key < $baselen){

                            $tmp = $oll;
                            $newCombination = array_slice($tmp,0);
                            $newCombination[]=$b;
                            $newCombs[] = array_slice($newCombination,0);
                        }

                }
            }

        }

    }

    return $newCombs;


}

echo "<pre>";
print_r(getCombinations(array('a','b','c','d','e'),2));
?>

参考:Php recursion to get all possibilities of strings

【讨论】:

  • 好的,谢谢 Prasanth Bendra
【解决方案2】:
<?php

 $n = array ('a', 'b', 'c', 'd', 'e');

 function getPerm($array, $key)
 {
   foreach( $array as $arrkey=>$value )
   {
      if($arrkey != $key)
      {
        echo " $array[$key]:$array[$arrkey] ";
      }
   }
 }


foreach($n as $key=>$value)
{
  getPerm($n, $key);
}

?>

见键盘http://codepad.org/uTaW8ssx

【讨论】:

  • 相反,我希望输出为:ab,ac,ad,ae,bc,bd,be,cd,ce,de,abc,abd,abe,acd,ace,ade,bcd, bce, bde, cde, abcd, abce, abde, acde, bcde, abcde,
【解决方案3】:

Java:

ArrayList<String> setProduct (ArrayList<String> a, ArrayList<String> b)
{
    ArrayList<String> prod = new ArrayList<String>();

    for (String s : a)
    {
        for (String t : b)
        {
            prod.add(s + ":" + t);
        }
    }

    return prod;
}

PHP:

<?php
function setProduct($a, $b)
{
    $arr = array();

    foreach ($a as $s)
    {
        foreach ($b as $t)
        {
            $arr[] = $s . ":" . $t;
        }
    }

    return $arr;
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2021-07-16
    • 1970-01-01
    • 2013-02-21
    相关资源
    最近更新 更多