2011-03-06 3 views
0

접두어 배열, 기본 단어 배열 및 접미어 배열이 있습니다. 내가 할 수있는 모든 조합을보고 싶습니다.순열/조합 접두어와 접미사 생성

예 :

prefixes: 1 2 
    words: hello test 
    suffixes: _x _y 

    Results: 

1hello_x 
1hello_y 
1hello 
1test_x 
1test_y 
1test  
1_x  
1_y  
1   
2hello_x 
2hello_y 
2hello 
2test_x 
2test_y 
2test 
2_x  
2_y  
2  
hello_x 
hello_y 
hello 
test_x 
test_y 
test  
_x  
_y  
y 

내가 어떻게 할 수 있습니까?

편집 : 모든 응답 주셔서 감사합니다. 해결책을 찾아 보겠습니다.하지만 접두사가없는 경우 조합이 실패합니다. 접두사가 없어도 기본 단어와 접미사를 계속 따라 가야합니다.

답변

0
function combineAll ($prefixes, $words, $suffixes) 
{ 
    $combinations = array(); 
    foreach ($prefixes as $prefix) 
    { 
    foreach ($words as $word) 
    { 
     foreach ($suffixes as $suffix) 
     { 
     $combinations[] = $prefix.$word.$suffix; 
     } 
    } 
    } 
    return $combinations; 
} 
+0

편집을 참조하십시오. – ParoX

+0

'array_push ($ prefixes, ");'..''array_push ($ words,");''array_push ($ prefixes, ");'를 추가하면 필요한 것을 할 수 있습니다. 또한주의 할 점은 매개 변수에 $ 접미사가 붙고 접미사가 – ParoX

+0

일 뿐이라는 점입니다. '전체'질문을 여러 번 바꾸지 말고 첫 번째 장소에 게시하십시오. –

0

이 당신을 시작할 수 있어야합니다

http://ask.amoeba.co.in/php-combinations-of-array-elements/

//$a = array("1", "2"); 
$b = array("hello", "test"); 
$c = array("_x", "_y"); 

if(is_array($a)){ 
$aG = array($a,$b, $c); 
}else{ 
$aG = array($b, $c); 
    } 
$codes = array(); 
$pos = 0; 
generateCodes($aG); 

function generateCodes($arr) { 
    global $codes, $pos; 
    if(count($arr)) { 
     for($i=0; $i<count($arr[0]); $i++) { 
      $tmp = $arr; 
      $codes[$pos] = $arr[0][$i]; 
      $tarr = array_shift($tmp); 
      $pos++; 
      generateCodes($tmp); 

     } 
    } else { 
     echo join("", $codes)."<br/>"; 
    } 
    $pos--; 
} 

결과 :
1hello_x
1hello_y
1test_x
1test_y
2hello_x
2hello_y
2test_x
2test_y

+0

편집을 참조하십시오 PHP에서이 작업을 수행 할 내장 기능이없는 생각, 당신이 원하는 것을 할 것입니다. – ParoX

+0

(선택 사항) $ a –

0
for each $prefix in $prefixes { 
for each $base in $basewords { 
for each $suffix in $suffixes { 
echo $prefix.$base.$suffix."\n" 
}}} 

이 나는 ​​(파이썬이 있지만)

+0

편집 허용을 참조하십시오. – ParoX

관련 문제