我需要在代码点火器 php 中创建类似 woo commerce 的变体



我有一个这样的数组(从数据库动态生成的数组(

$attr = array('color'=>array('red','pink','yello','white','black','light-yellow','maroon','neal'),"brand"=>array('nike','adidas','dg','puma','neaon'),"size"=>array(8,10,11,12,13,14,15));

我需要这样的结果

red-nike-8
red-nike-9
red-nike-10
red-nike-11
red-nike-12
red-nike-13
red-nike-14
red-nike-15
red-adidas-8
red-adidas-9
red-adidas-10
red-adidas-11
red-adidas-12
red-adidas-13
red-adidas-14
red-adidas-15
red-dg-8
red-dg-9
red-dg-10
red-dg-11
red-dg-12
red-dg-13
red-dg-14
red-dg-15
red-puma-8
red-puma-9
red-puma-10
red-puma-11
red-puma-12
red-puma-13
red-puma-14
red-puma-15
red-neaon-8
red-neaon-9
red-neaon-10
red-neaon-11
red-neaon-12
red-neaon-13
red-neaon-14
red-neaon-15
pink-nike-8
pink-nike-9
pink-nike-10
pink-nike-11
pink-nike-12
pink-nike-13
pink-nike-14
pink-nike-15
pink-adidas-8
pink-adidas-9
pink-adidas-10
pink-adidas-11
pink-adidas-12
pink-adidas-13
pink-adidas-14
pink-adidas-15........

下面是我的代码,这是静态的,但我需要开发来创建动态结构。

$attr_color = array('red','pink','yello','white','black','light-yellow','maroon','neal');
$attr_brand = array('nike','adidas','dg','puma','neaon');
$attr_size = array(8,10,11,12,13,14,15);
$array = array();
foreach ($attr_color as $key => $value_one)
{
foreach ($attr_brand as $key => $value_two) 
{
foreach ($attr_size as $key => $value_three) 
{
$array[] = array($value_one,$value_two,$value_three);
}
}
}

正如您在初始中提到的动态数组,考虑到它是一个包含多个子数组的深度二数组,如下面的代码所示。您可以在其中添加任何其他子数组,就像我添加其他数组一样。

$attr = [
[
'color1',
'color2',
'color3',
],
[
'brand1',
'brand2',
'brand3',
],
[
'size1',
'size2',
'size3',
],
[
'other1',
'other2',
'other3',
],
];

您必须像这样循环才能达到预期的结果。

function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return [];
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
// get combinations from subsequent arrays
$tmp = combinations($arrays, $i + 1);
$result = [];
// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ? 
array_merge([$v], $t) :
[$v, $t];
}
}
$finalArray = [];
foreach($result as $k => $val){
$finalArray[$k] = implode("-",$val);
}   
return $finalArray;
}
$result = combinations($attr);
print_r($result);

最终结果如下。

Array
(
[0] => color1-brand1-size1-other1
[1] => color1-brand1-size1-other2
[2] => color1-brand1-size1-other3
[3] => color1-brand1-size2-other1
[4] => color1-brand1-size2-other2
[5] => color1-brand1-size2-other3
[6] => color1-brand1-size3-other1
[7] => color1-brand1-size3-other2
[8] => color1-brand1-size3-other3
[9] => color1-brand2-size1-other1
[10] => color1-brand2-size1-other2
[11] => color1-brand2-size1-other3
[12] => color1-brand2-size2-other1
[13] => color1-brand2-size2-other2
[14] => color1-brand2-size2-other3
[15] => color1-brand2-size3-other1
[16] => color1-brand2-size3-other2
[17] => color1-brand2-size3-other3
[18] => color1-brand3-size1-other1
[19] => color1-brand3-size1-other2
[20] => color1-brand3-size1-other3
[21] => color1-brand3-size2-other1
[22] => color1-brand3-size2-other2
[23] => color1-brand3-size2-other3
[24] => color1-brand3-size3-other1
[25] => color1-brand3-size3-other2
[26] => color1-brand3-size3-other3
[27] => color2-brand1-size1-other1
[28] => color2-brand1-size1-other2
[29] => color2-brand1-size1-other3
[30] => color2-brand1-size2-other1
[31] => color2-brand1-size2-other2
[32] => color2-brand1-size2-other3
[33] => color2-brand1-size3-other1
[34] => color2-brand1-size3-other2
[35] => color2-brand1-size3-other3
[36] => color2-brand2-size1-other1
[37] => color2-brand2-size1-other2
[38] => color2-brand2-size1-other3
[39] => color2-brand2-size2-other1
[40] => color2-brand2-size2-other2
[41] => color2-brand2-size2-other3
[42] => color2-brand2-size3-other1
[43] => color2-brand2-size3-other2
[44] => color2-brand2-size3-other3
[45] => color2-brand3-size1-other1
[46] => color2-brand3-size1-other2
[47] => color2-brand3-size1-other3
[48] => color2-brand3-size2-other1
[49] => color2-brand3-size2-other2
[50] => color2-brand3-size2-other3
[51] => color2-brand3-size3-other1
[52] => color2-brand3-size3-other2
[53] => color2-brand3-size3-other3
[54] => color3-brand1-size1-other1
[55] => color3-brand1-size1-other2
[56] => color3-brand1-size1-other3
[57] => color3-brand1-size2-other1
[58] => color3-brand1-size2-other2
[59] => color3-brand1-size2-other3
[60] => color3-brand1-size3-other1
[61] => color3-brand1-size3-other2
[62] => color3-brand1-size3-other3
[63] => color3-brand2-size1-other1
[64] => color3-brand2-size1-other2
[65] => color3-brand2-size1-other3
[66] => color3-brand2-size2-other1
[67] => color3-brand2-size2-other2
[68] => color3-brand2-size2-other3
[69] => color3-brand2-size3-other1
[70] => color3-brand2-size3-other2
[71] => color3-brand2-size3-other3
[72] => color3-brand3-size1-other1
[73] => color3-brand3-size1-other2
[74] => color3-brand3-size1-other3
[75] => color3-brand3-size2-other1
[76] => color3-brand3-size2-other2
[77] => color3-brand3-size2-other3
[78] => color3-brand3-size3-other1
[79] => color3-brand3-size3-other2
[80] => color3-brand3-size3-other3
)

谢谢

相关内容

  • 没有找到相关文章

最新更新