2014-09-12 2 views
0

내 입력을 생성하는 데 도움이 필요합니다.배열 설정 값의 모든 조합

제품 :

$products = array('prod_1'=>1,'prod_2'=>1,'prod_3'=>1,'prod_4'=>1); 

가격 :

나는 배열의 두 세트를 가지고 내가 좀하고 싶습니다 무엇

$prices = array(1,2,3); 

은, 루프는 출력 것입니다 $ prices로 채워진 $ products 배열의 가능한 모든 조합 :

예를 출력

# array('prod_1'=>1,'prod_2'=>1,'prod_3'=>1,'prod_4'=>1); 
# array('prod_1'=>2,'prod_2'=>1,'prod_3'=>1,'prod_4'=>1); 
# array('prod_1'=>1,'prod_2'=>2,'prod_3'=>1,'prod_4'=>1); 
# array('prod_1'=>1,'prod_2'=>1,'prod_3'=>2,'prod_4'=>1); 
# array('prod_1'=>1,'prod_2'=>1,'prod_3'=>1,'prod_4'=>2); 
... 
# array('prod_1'=>1,'prod_2'=>2,'prod_3'=>2,'prod_4'=>1); 
... 
# array('prod_1'=>2,'prod_2'=>3,'prod_3'=>1,'prod_4'=>1); 
... 
#1 array('prod_1'=>3,'prod_2'=>1,'prod_3'=>3,'prod_4'=>2); 
etc. 

UPDATE는 시계 같은 작업을해야 제 생각에는 1

:

  1. $에서 첫 번째 값에 $ 제품에서 모든 값을 설정을 가격
  2. 루프는 $ products에 대한 $ prices의 모든 값을 생각했습니다 [ 'pro d_1 ']
  3. $ products ['prod_1 ']을 $ prices [0]으로 설정하고 $ products ['prod_2 '] (index + 1)을 가리키면
  4. 포인트 2와 3 올바른 트랙에

    1,1,1,1 
    2,1,1,1 
    3,1,1,1 
    1,2,1,1 
    2,2,1,1 
    3,2,1,1 
    1,3,1,1 
    2,3,1,1 
    3,3,1,1 
    1,1,2,1 
    do it until: 
    3,3,3,3 
    

    암 : 모든 $ 제품 값은 가격이

출력 가치 $ 마지막으로 설정 될 때까지 인덱스 + 1, 이전 재설정 이동?

+0

당신은 이미 루프를 언급 한

여기 내 코드입니다. 그것을 시도하고 게시하십시오. – AbraCadaver

답변

1

좋아, 알아 냈어.

$t = new test(); 
$t->run(); 

class test{ 
    private $_data = array("p1"=>1,"p2"=>1,"p3"=>1); 
    private $_values =array(1,2,3); 
    private $_data_pos =0; 
    private $_values_pos =0; 


public function run(){ 
    while($this->combos()==true){ 
     // do sth with $this->_data 
     echo "<pre>"; 
     var_dump($this->_data); 
     echo "</pre>"; 
    } 
} 

function combos(){ 
    $keys = array_keys($this->_data); 
    if($this->_values_pos>count($this->_values)-1){ 
     $this->_values_pos = 0; 

     while($this->_data[$keys[$this->_data_pos]]==$this->_values[count($this->_values)-1]){ 

      $this->_data[$keys[$this->_data_pos]] = $this->_values[0]; 
      $this->_data_pos++; 
      if(empty($keys[$this->_data_pos])) return false; 

     } 
     $k = array_search($this->_data[$keys[$this->_data_pos]],$this->_values); 
     $this->_data[$keys[$this->_data_pos]] = $this->_values[$k+1]; 
     $this->_data_pos=0; 

      //return true; 
    } 
    $this->_data[$keys[$this->_data_pos]] = $this->_values[$this->_values_pos]; 

    $this->_values_pos++; 

    return true; 
} 

} 

그것은 출력 :

array(3) { 
    ["p1"]=> 
    int(1) 
    ["p2"]=> 
    int(1) 
    ["p3"]=> 
    int(1) 
} 
array(3) { 
    ["p1"]=> 
    int(2) 
    ["p2"]=> 
    int(1) 
    ["p3"]=> 
    int(1) 
} 
array(3) { 
    ["p1"]=> 
    int(3) 
    ["p2"]=> 
    int(1) 
    ["p3"]=> 
    int(1) 
} 
array(3) { 
    ["p1"]=> 
    int(1) 
    ["p2"]=> 
    int(2) 
    ["p3"]=> 
    int(1) 
} 
array(3) { 
    ["p1"]=> 
    int(2) 
    ["p2"]=> 
    int(2) 
    ["p3"]=> 
    int(1) 
} 
array(3) { 
    ["p1"]=> 
    int(3) 
    ["p2"]=> 
    int(2) 
    ["p3"]=> 
    int(1) 
} 
array(3) { 
    ["p1"]=> 
    int(1) 
    ["p2"]=> 
    int(3) 
    ["p3"]=> 
    int(1) 
}