Use three nested foreach and push to a $result
array at the last level. (Here I used the compact
function to reduce the code.)
$provided = [
'Shirt' => [
'color' => ['green', 'red'],
'size' => ['Small', 'Medium'],
],
]; // Reduced the provided data to reduce the output for sample purposes.
$result = [];
foreach ($provided as $type => $attributes) {
foreach ($attributes['color'] as $color) {
foreach ($attributes['size'] as $size) {
$result[] = compact('type','color','size');
}
}
}
var_dump($result);
Will output
array(4) {
[0]=>
array(3) {
["type"]=>
string(5) "Shirt"
["color"]=>
string(5) "green"
["size"]=>
string(5) "Small"
}
[1]=>
array(3) {
["type"]=>
string(5) "Shirt"
["color"]=>
string(5) "green"
["size"]=>
string(6) "Medium"
}
[2]=>
array(3) {
["type"]=>
string(5) "Shirt"
["color"]=>
string(3) "red"
["size"]=>
string(5) "Small"
}
[3]=>
array(3) {
["type"]=>
string(5) "Shirt"
["color"]=>
string(3) "red"
["size"]=>
string(6) "Medium"
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…