2013-03-25 3 views
4

есть все, что может помочь мне получить результат, как:сочетание массива данных

а: Ь а: с A: D а: е б: B: d б: е C: d с: е д: е а: б: в A: B: d A: B: е а: с: d а: с: е A: d: е б : c: d b: c: e b: d: e C: D: E A: B: C: D A: B: C: E A: B: D: E а: с: d: е B: C: D: E а: Ь : c: d: e

массив данных $ n = массив ('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 />';    
       }    
      }   
     }  
    } 
} 

, но я думаю, что код слишком длинный, пожалуйста, помогите упростить,

благодаря

+0

Использование рекурсивной функции. – sectus

ответ

0

Try это:

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

Ref: Php recursion to get all possibilities of strings

+0

ОК, спасибо Prasanth Bendra –

0
<?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); 
} 

?> 

см codepad http://codepad.org/uTaW8ssx

+0

вместо этого, я хочу, вывод как: AB, переменных тока, объявлений, ае, Ьса, шда, быть, кд, с, де, азбуки, абда, Абэ, ДС , туз, ADE, BCD, BCE, BDE, CDE, ABCD, ABCE, ABDE, AcdE, b c d e, a b c d e, –

+0

позвольте мне проверить ..... – alwaysLearn

0

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; 
} 
?> 
Смежные вопросы