1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| function combination(array $options) { $rows = [];
foreach ($options as $option => $items) { if (count($rows) > 0) { $clone = $rows;
$rows = [];
foreach ($items as $item) { $tmp = $clone; foreach ($tmp as $index => $value) { $value[$option] = $item; $tmp[$index] = $value; }
$rows = array_merge($rows, $tmp); } } else { foreach ($items as $item) { $rows[][$option] = $item; } } }
return $rows; }
$options = array( 'sex' => [1, 2], 'area' => [1, 2, 3, 4, 5, 6, 7, 8, 9], 'level' => [1, 2, 3, 4], ); $rows = combination($options);
|