$a = [
'color' => ['red', 'blue'],
'size' => ['10-12', '12-14']
];
$b = [
['color' => 'red', 'size' => '10-12'],
['color' => 'blue', 'size' => '10-12'],
['color' => 'red', 'size' => '12-14'],
['color' => 'blue', 'size' => '12-14'],
];
<?php
$a = [
'color' => ['red', 'blue', 'orange'],
'size' => ['10-12', '12-14', '13-13'],
'somekey' => ['val1', 'val2', 'val3'],
];
function addValue(&$arr, $key, $val)
{
if (!count($arr)) {
$arr[] = [$key => $val];
return;
}
foreach($arr as $num => $values) {
$arr[$num][$key] = $val;
}
}
$b = [];
foreach($a as $key => $values) {
$copyB = $b;
$newB = [];
foreach($values as $value) {
$currentB = $copyB;
addValue($currentB, $key, $value);
$newB = array_merge($newB, $currentB);
}
$b = $newB;
}
print_r($b);
function crossJoin($array)
{
$result = [];
$current = array_splice($array, -1);
foreach ($current as $key => $values) {
foreach ((array) $values as $value) {
if (empty($array)) {
array_push($result, [$key => $value]);
} else {
foreach (crossJoin($array) as $temp) {
array_push($result, array_merge([$key => $value], $temp));
}
}
}
}
return $result;
}
crossJoin($a);