Php 数组集值的所有组合

Php 数组集值的所有组合,php,arrays,algorithm,combinations,Php,Arrays,Algorithm,Combinations,我需要一些帮助来为我的函数生成输入 我有两组阵列: 产品: $products = array('prod_1'=>1,'prod_2'=>1,'prod_3'=>1,'prod_4'=>1); $prices = array(1,2,3); 价格: $products = array('prod_1'=>1,'prod_2'=>1,'prod_3'=>1,'prod_4'=>1); $prices = array(1,2,3); 我想要得

我需要一些帮助来为我的函数生成输入

我有两组阵列:

产品:

$products = array('prod_1'=>1,'prod_2'=>1,'prod_3'=>1,'prod_4'=>1);
$prices = array(1,2,3);
价格:

$products = array('prod_1'=>1,'prod_2'=>1,'prod_3'=>1,'prod_4'=>1);
$prices = array(1,2,3);
我想要得到的是一个循环,它将输出由$price填充的$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.
更新1

在我看来,它应该像时钟一样工作:

  • 将$products中的所有值设置为$prices中的第一个值
  • 循环考虑$prod_1产品的$prices中的所有值
  • 当您将$products['prod_1']设置为$prices[0]并对$products['prod_2']执行第2点时(索引+1)
  • 执行第2点和第3点并执行索引+1,重置prev,直到所有$products值都设置为last$prices值
  • 输出:

    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
    
    我在正确的轨道上吗?

    好的,我知道了

    这是我的密码:

    $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;
    }
    
    }
    

    您已经提到了一个循环。给它一次机会,然后试一下。