[英]Create a product array with all possible variations
I'm trying to produce an array with all variations of a product.我正在尝试生成一个包含产品所有变体的数组。 There can be an unlimited amount of attributes and variations.
可以有无限数量的属性和变化。
Provided array:提供的数组:
["Shirt"] # Product
["Color"] # Attribute
["Green"] # Variation
["Red"]
["Blue"]
["Size"]
["Small"]
["Medium"]
["Large"]
Expected result:预期结果:
[0]
["type" => "Shirt"]
["color" => "Green"]
["Size" => "Small"]
[1]
["type" => "Shirt"]
["color" => "Green"]
["Size" => "Medium"]
...
[4]
["type" => "Shirt"]
["color" => "Red"]
["Size" => "Medium"]
[4]
["type" => "Shirt"]
["color" => "Red"]
["Size" => "Large"]
I've tried Laravel's and PHP's built-in functions, but haven't found success.我尝试过 Laravel 和 PHP 的内置函数,但没有成功。 I would really appreciate any insight.
我真的很感激任何见解。
Use three nested foreach and push to a $result
array at the last level.使用三个嵌套的foreach并推送到最后一级的
$result
数组。 (Here I used the compact
function to reduce the code.) (这里我使用了
compact
函数来减少代码。)
$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"
}
}
Your question seems to be close to this universal approach I have:你的问题似乎接近我拥有的这种通用方法:
$arr = [
'size' => ['XS', 'S', 'M'],
'color' => ['yellow', 'brown', 'white'],
'weight'=> [
'light' => [
'super',
'medium'
],
'normal',
'heavy' => [
'regular',
'most',
'overload'
]
]
];
function variations($array) {
if (empty($array)) {
return [];
}
function traverse($array, $parent_ind) {
$r = [];
$pr = '';
if(!is_numeric($parent_ind)) {
$pr = $parent_ind.'-';
}
foreach ($array as $ind => $el) {
if (is_array($el)) {
$r = array_merge($r, traverse($el, $pr.(is_numeric($ind) ? '' : $ind)));
} elseif (is_numeric($ind)) {
$r[] = $pr.$el;
} else {
$r[] = $pr.$ind.'-'.$el;
}
}
return $r;
}
//1. Go through entire array and transform elements that are arrays into elements, collect keys
$keys = [];
$size = 1;
foreach ($array as $key => $elems) {
if (is_array($elems)) {
$rr = [];
foreach ($elems as $ind => $elem) {
if (is_array($elem)) {
$rr = array_merge($rr, traverse($elem, $ind));
} else {
$rr[] = $elem;
}
}
$array[$key] = $rr;
$size *= count($rr);
}
$keys[] = $key;
}
//2. Go through all new elems and make variations
$rez = [];
for ($i = 0; $i < $size; $i++) {
$rez[$i] = [];
foreach ($array as $key => $value) {
$current = current($array[$key]);
$rez[$i][$key] = $current;
}
foreach ($keys as $key) {
if (!next($array[$key])) {
reset($array[$key]);
} else {
break;
}
}
}
return $rez;
}
die(print_r(variations($arr)));
Result
will be an array of all possible combinations of attributes
or whatever (see below). Result
将是所有可能的attributes
组合或其他任何组合的数组(见下文)。 I used it for creating Test Woocommerce Variable
products.我用它来创建
Test Woocommerce Variable
产品。
Testing started at 11:37 ...
/usr/bin/php /usr/local/bin/phpunit --bootstrap /opt/lampp/htdocs/unittests.lo/wp-content/plugins/test-woocommerce-izettle/tests/bootstrap.php --configuration /opt/lampp/htdocs/unittests.lo/wp-content/plugins/test-woocommerce-izettle/phpunit.xml.dist --teamcity
Array
(
[0] => Array
(
[size] => XS
[color] => yellow
[weight] => light-super
)
[1] => Array
(
[size] => S
[color] => yellow
[weight] => light-super
)
[2] => Array
(
[size] => M
[color] => yellow
[weight] => light-super
)
[3] => Array
(
[size] => XS
[color] => brown
[weight] => light-super
)
[4] => Array
(
[size] => S
[color] => brown
[weight] => light-super
)
[5] => Array
(
[size] => M
[color] => brown
[weight] => light-super
)
[6] => Array
(
[size] => XS
[color] => white
[weight] => light-super
)
[7] => Array
(
[size] => S
[color] => white
[weight] => light-super
)
[8] => Array
(
[size] => M
[color] => white
[weight] => light-super
)
[9] => Array
(
[size] => XS
[color] => yellow
[weight] => light-medium
)
[10] => Array
(
[size] => S
[color] => yellow
[weight] => light-medium
)
[11] => Array
(
[size] => M
[color] => yellow
[weight] => light-medium
)
[12] => Array
(
[size] => XS
[color] => brown
[weight] => light-medium
)
[13] => Array
(
[size] => S
[color] => brown
[weight] => light-medium
)
[14] => Array
(
[size] => M
[color] => brown
[weight] => light-medium
)
[15] => Array
(
[size] => XS
[color] => white
[weight] => light-medium
)
[16] => Array
(
[size] => S
[color] => white
[weight] => light-medium
)
[17] => Array
(
[size] => M
[color] => white
[weight] => light-medium
)
[18] => Array
(
[size] => XS
[color] => yellow
[weight] => normal
)
[19] => Array
(
[size] => S
[color] => yellow
[weight] => normal
)
[20] => Array
(
[size] => M
[color] => yellow
[weight] => normal
)
[21] => Array
(
[size] => XS
[color] => brown
[weight] => normal
)
[22] => Array
(
[size] => S
[color] => brown
[weight] => normal
)
[23] => Array
(
[size] => M
[color] => brown
[weight] => normal
)
[24] => Array
(
[size] => XS
[color] => white
[weight] => normal
)
[25] => Array
(
[size] => S
[color] => white
[weight] => normal
)
[26] => Array
(
[size] => M
[color] => white
[weight] => normal
)
[27] => Array
(
[size] => XS
[color] => yellow
[weight] => heavy-regular
)
[28] => Array
(
[size] => S
[color] => yellow
[weight] => heavy-regular
)
[29] => Array
(
[size] => M
[color] => yellow
[weight] => heavy-regular
)
[30] => Array
(
[size] => XS
[color] => brown
[weight] => heavy-regular
)
[31] => Array
(
[size] => S
[color] => brown
[weight] => heavy-regular
)
[32] => Array
(
[size] => M
[color] => brown
[weight] => heavy-regular
)
[33] => Array
(
[size] => XS
[color] => white
[weight] => heavy-regular
)
[34] => Array
(
[size] => S
[color] => white
[weight] => heavy-regular
)
[35] => Array
(
[size] => M
[color] => white
[weight] => heavy-regular
)
[36] => Array
(
[size] => XS
[color] => yellow
[weight] => heavy-most
)
[37] => Array
(
[size] => S
[color] => yellow
[weight] => heavy-most
)
[38] => Array
(
[size] => M
[color] => yellow
[weight] => heavy-most
)
[39] => Array
(
[size] => XS
[color] => brown
[weight] => heavy-most
)
[40] => Array
(
[size] => S
[color] => brown
[weight] => heavy-most
)
[41] => Array
(
[size] => M
[color] => brown
[weight] => heavy-most
)
[42] => Array
(
[size] => XS
[color] => white
[weight] => heavy-most
)
[43] => Array
(
[size] => S
[color] => white
[weight] => heavy-most
)
[44] => Array
(
[size] => M
[color] => white
[weight] => heavy-most
)
[45] => Array
(
[size] => XS
[color] => yellow
[weight] => heavy-overload
)
[46] => Array
(
[size] => S
[color] => yellow
[weight] => heavy-overload
)
[47] => Array
(
[size] => M
[color] => yellow
[weight] => heavy-overload
)
[48] => Array
(
[size] => XS
[color] => brown
[weight] => heavy-overload
)
[49] => Array
(
[size] => S
[color] => brown
[weight] => heavy-overload
)
[50] => Array
(
[size] => M
[color] => brown
[weight] => heavy-overload
)
[51] => Array
(
[size] => XS
[color] => white
[weight] => heavy-overload
)
[52] => Array
(
[size] => S
[color] => white
[weight] => heavy-overload
)
[53] => Array
(
[size] => M
[color] => white
[weight] => heavy-overload
)
)
Process finished with exit code 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.