1

我有很多作为产品选项的数组(下面的示例):

$option_array[] = array(
    "name" => "delivery",
    "options" => array(
        "sat", "sun")
);
$option_array[] = array(
    "name" => "colour",
    "options" => array(
        "red", "blue", "green")
);
$option_array[] = array(
    "name" => "size",
    "options" => array(
        "small", "medium", "large")
);

什么是我想出每个排列作为 foreach 循环但相当有效的最佳方式。我需要在数据库中为每个可能的组合输入一个值(在这个例子中应该是 18 个值或 18 次迭代)。如何获得结果排列的数组?

预期输出:

array(18){
    [0] => array(3){
        "delivery" => "sat",
        "colour" => "red",
        "size" => "small",
    }
    [1] => array(3){
        "delivery" => "sat",
        "colour" => "red",
        "size" => "medium",
    }
    [2] => array(3){
        "delivery" => "sat",
        "colour" => "red",
        "size" => "large",
    }
    [3] => array(3){
        "delivery" => "sat",
        "colour" => "blue",
        "size" => "small",
    }
    [4] => array(3){
        "delivery" => "sat",
        "colour" => "blue",
        "size" => "medium",
    }
    [5] => array(3){
        "delivery" => "sat",
        "colour" => "blue",
        "size" => "large",
    }
    [6] => array(3){
        "delivery" => "sat",
        "colour" => "green",
        "size" => "small",
    }
    [7] => array(3){
        "delivery" => "sat",
        "colour" => "green",
        "size" => "medium",
    }
    [8] => array(3){
        "delivery" => "sat",
        "colour" => "green",
        "size" => "large",
    }
    [9] => array(3){
        "delivery" => "",
        "colour" => "red",
        "size" => "medium",
    }        [10] => array(3){
        "delivery" => "sun",
        "colour" => "red",
        "size" => "small",
    }
    [11] => array(3){
        "delivery" => "sun",
        "colour" => "red",
        "size" => "medium",
    }
    [12] => array(3){
        "delivery" => "sun",
        "colour" => "red",
        "size" => "large",
    }
    [13] => array(3){
        "delivery" => "sun",
        "colour" => "blue",
        "size" => "small",
    }
    [14] => array(3){
        "delivery" => "sun",
        "colour" => "blue",
        "size" => "medium",
    }
    [15] => array(3){
        "delivery" => "sun",
        "colour" => "blue",
        "size" => "large",
    }
    [16] => array(3){
        "delivery" => "sun",
        "colour" => "green",
        "size" => "small",
    }
    [17] => array(3){
        "delivery" => "sun",
        "colour" => "green",
        "size" => "medium",
    }
    [18] => array(3){
        "delivery" => "sun",
        "colour" => "green",
        "size" => "large",
    }
}

我并不过分担心输出格式。

4

1 回答 1

0

这适用于http://writecodeonline.com/php/

$option_array[] = array(
    "name" => "delivery",
    "options" => array(
        "sat", "sun")
);
$option_array[] = array(
    "name" => "colour",
    "options" => array(
        "red", "blue", "green")
);
$option_array[] = array(
    "name" => "size",
    "options" => array(
        "small", "medium", "large")
);

$N = 1;
foreach ($option_array as $options) $N *= count($options['options']);

$all_combinations = array();

for( $i = 0 ; $i < $N ; ++$i )
{
  $all_combinations[$i] = array();
  $q = $i;
  for( $j = count($option_array)-1 ; 0 <= $j ; --$j )
  {
    $opt = $option_array[$j];
    $nopts = count($opt['options']);
    $all_combinations[$i][$opt['name']] = $opt['options'][$q % $nopts];
    $q = floor($q / $nopts);
  }
}

print_r( $all_combinations );
于 2013-09-06T20:33:10.017 回答