Featured image of post PHP Array - 各種可能的組合

PHP Array - 各種可能的組合

intro

完全來自 文件 使用,
根據取到array,組出所有的組合。

main

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19

function getCombinations($arrays) {
	$result = [[]];
	foreach ($arrays as $property => $property_values) {
		$tmp = [];
		foreach ($result as $result_item) {
			foreach ($property_values as $property_value) {
				$tmp[] = array_merge($result_item, [$property => $property_value]);
			}
		}
		$result = $tmp;
	}
	return $result;
}

$combinations = getCombinations([
   1 => ['a1', 'a2', 'a3'],
   2 => ['b1', 'b2', 'b3'],
]);

example-output

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy