php

2012-09-11 12 views
6

में एक्स पॉव y के सभी संयोजन मैं एक सरणी होने तीन तत्व $base =(#m, #f,#p)php

मैं की तरह $var = (s1, s2)

तत्व के किसी भी संख्या होने एक दूसरे सरणी है है अब मैं सभी संभव संयोजनों बनाने की जरूरत आधार सरणी पर निर्भर करता है। मुझे मिली सूत्र x pow y है।

इस उदाहरण में मेरी आधार सरणी तीन तत्व है और $var है, तो pow(3, 2) है। मुझे इन नौ संयोजनों की आवश्यकता है। i.e

#m#m #m#f #m#p 
#f#m #f#f #f#p 
#p#m #p#f #p#p 

दूसरी सरणी में तत्वों की संख्या जेनरेट किए गए संयोजनों की लंबाई प्रभावी रूप से होती है। इस उदाहरण में के रूप में 2 सरणी की लंबाई 2 इसलिए सभी उत्पन्न तार (दिए गए उदाहरण के लिए) लंबाई 2.

+0

आपको केवल एक नेस्टेड लूप और 'एन-डी सरणी' की आवश्यकता है। – hjpotter92

+1

दूसरी सरणी पहले से कैसे संबंधित है? मैं नहीं देखता कि यह कैसे प्रासंगिक है। – DaveRandom

+0

@DaveRandom 3 मानों के साथ दूसरी सरणी पर विचार करें। परिणाम 3x3x3 सरणी होगी। – hjpotter92

उत्तर

4

आप इस प्रकार का पुनरावर्ती क्रिया का उपयोग कर सकते हैं:

// input definition 
$baseArray = array("#m", "#f", "#p"); 
$varArray = array("s1", "s2", "s3"); 

// call the recursive function using input 
$result = recursiveCombinations($baseArray, sizeof($varArray)); 

// loop over the resulting combinations 
foreach($result as $r){ 
    echo "<br />combination " . implode(",", $r); 
} 

// this function recursively generates combinations of #$level elements 
// using the elements of the $base array 
function recursiveCombinations($base, $level){ 
    $combinations = array(); 
    $recursiveResults = array(); 

    // if level is > 1, get the combinations of a level less recursively 
    // for level 1 the combinations are just the values of the $base array 
    if($level > 1){ 
     $recursiveResults = recursiveCombinations($base, --$level); 
    }else{ 
     return $base; 
    } 
    // generate the combinations 
    foreach($base as $baseValue){ 
     foreach($recursiveResults as $recursiveResult){ 
      $combination = array($baseValue); 
      $combination = array_merge($combination, (array)$recursiveResult); 
      array_push($combinations, $combination); 
     } 
    } 
    return $combinations; 
} 

Working codepad demo

+0

बिल्कुल सही, मैं यह मेरे लिए बहुत अच्छा काम देख सकता हूं। मैं इस पर काम करूंगा और अगर कोई समस्या पाई तो इसे अपडेट करें। यू आर सुपरबर :) – user1635914

+0

बढ़िया, खुश मैं मदद कर सकता था! सौभाग्य! – Asciiom

0
<?php 
    $strs = Array("some", "thing", "here"); 
    $poss = Array(1, 2); 
    $new = Array(); 
    for($i = 0; $i < pow(count($strs), count($poss)); $i++) { 
     for($j = 0; $j < count($strs); $j++) { 
      $new[$i] = $strs[($i%count($strs))] . " " . $strs[$j]; 
     } 
    } 
    print_r($new); 
?> 

कार्य codepad लिंक है।

लिंक

+1

यदि आप $ poss = Array (1, 2,3) बदलते हैं, तो लंबाई तीन हो गई है, इसे लंबाई तीन का मूल्य उत्पन्न करना चाहिए, जबकि यह कोड लंबाई 2 के मूल्य उत्पन्न कर रहा है, चाहे $ – user1635914

+0

के आकार के बावजूद यह होना चाहिए यह [0] => कुछ कुछ [1] => कुछ चीज़ ..... तो – user1635914

+0

पर यह काम नहीं प्रतीत होता है, जब तक कि मैं इस सवाल को गलत नहीं समझता। यदि आप ओपी के इनपुट का उपयोग करते हैं तो आपको 3 संयोजनों के 3 समान सेट मिलते हैं। – Asciiom