Array combination calculates all the combination forms?
array(2) {
[0] => array(3) {
[0] => string(3) "16G"
[1] => string(3) "32G"
[2] => string(3) "64G"
}
[1] => array(2) {
[0] => string(1) "L"
[1] => string(2) "XL"
}
}
["16G","L"]
["16G","XL"]
["32G","L"]
["32G","XL"]
["64G","L"]
["64G","XL"]
array(6) {
[0] => array(3) {
[0] => string(3) "16G"
[1] => string(3) "L"
}
[1] => array(3) {
[0] => string(3) "16G"
[1] => string(3) "XL"
}
[2] => array(3) {
[0] => string(3) "32G"
[1] => string(3) "L"
}
[3] => array(3) {
[0] => string(3) "32G"
[1] => string(3) "XL"
}
[4] => array(3) {
[0] => string(3) "64G"
[1] => string(3) "L"
}
[5] => array(3) {
[0] => string(3) "64G"
[1] => string(3) "XL"
}
}
Matrix multiplication ?
js implementation of algorithm introduction-- n n matrix calculation
$arr = [["16G","32G","64G"],["L","XL"]];
$res = [];
for($i=0;$i<count($arr[0]);$iPP){
for($j=0;$j<count($arr[1]);$jPP){
$tem = [];
$tem[] = $arr[0][$i];
$tem[] = $arr[1][$j];
if($tem){
$res[] = $tem;
}
}
}
var_dump($res);
change my code in the js version answer to the php version as follows
$input = [["16G", "32G", "64G"], ["L", "XL"]];
$output = array_reduce($input, function($result, $cross_item){
if(!$result){
return array_map(function($item){
return [$item];
}, $cross_item);
}
return array_reduce($cross_item, function($acc, $target) use ($result){
return array_merge($acc, array_map(function($result_item) use ($target){
$result_item[] = $target;
return $result_item;
}, $result));
}, []);
}, []);
if you don't need to control the order, you only need the results, and you can use bit operations for full combination.
var rt=[];
const arr= [["16G", "32G", "64G"], ["L", "XL"]];
for(let i=0;i<8;iPP){
let a1=i & 3;//2
if(a1>2) continue;
let a2=i>>2;
let t=[arr[0][a1],arr[1][a2]];
rt.push(t);
}