【问题标题】:All combinations of array set values数组集合值的所有组合
【发布时间】:2014-09-12 17:18:31
【问题描述】:

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

我有两组数组:

产品:

$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.

更新 1

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

  1. 将 $products 中的所有值设置为 $prices 中的第一个值
  2. Lo​​op 认为 $prices 中 $products['prod_1'] 的所有值
  3. 当您将 $products['prod_1'] 设置为 $prices[0] 并为 $products['prod_2'] (index+1) 执行第 2 点时
  4. 执行第 2 点和第 3 点并执行 index+1,重置 prev,直到所有 $products 值都设置为最后一个 $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

我走在正确的轨道上吗?

【问题讨论】:

  • 你已经提到了一个循环。试一试并发布尝试。

标签: php arrays algorithm combinations


【解决方案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)
}

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 2012-03-27
    • 1970-01-01
    • 2014-02-11
    相关资源
    最近更新 更多