drl6054 2014-09-12 17:18 采纳率: 0%
浏览 35
已采纳

数组设置值的所有组合

I need some help in generating input for my function.

I have two sets of arrays:

Products:

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

Prices:

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

What i would like to get, is a loop that would output all possible combinations of $products array populated by $prices:

example output

# 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

In my opinion it should work something like a clock:

  1. Set all values in $products to first value from $prices
  2. Loop thought all values from $prices for $products['prod_1']
  3. When you bump the and set $products['prod_1'] to $prices[0] and do point 2 for $products['prod_2'] (index+1)
  4. Do point 2 and 3 and go index+1, reset prev until all $products values are set to last $prices value

Output:

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

Am in on the right track?

  • 写回答

1条回答 默认 最新

  • duanliang1898 2014-09-12 22:47
    关注

    Ok, I figured it out.

    Here's my code:

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

    It outputs:

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?