[英]create rules array based on a product variations array
I have this product variations array:我有这个产品变体数组:
array:3 [
0 => array:2 [
0 => "2_12"
1 => "4_9"
]
1 => array:2 [
0 => "2_3"
1 => "4_9"
]
2 => array:2 [
0 => "2_4"
1 => "4_10"
]
]
and need to create some rules from it.并且需要从中创建一些规则。
Each array contain a product variation and each element is made from option_value.每个数组都包含一个产品变体,每个元素都由 option_value 组成。
ex: the first array: [2_12, 4_9] => [color_purple, material_cotton]例如:第一个数组:[2_12, 4_9] => [color_purple, material_cotton]
I want to create another array that contains each element combination based on that array.我想创建另一个数组,其中包含基于该数组的每个元素组合。
ex: I want to see color_purple (2_12) what materials is combined with, material_cotton what colors is combined with...and so on.例如:我想看看 color_purple (2_12) 结合了什么材料,material_cotton colors 结合了什么......等等。
Like this:像这样:
array:5 [
"2_12" => array:1 [
0 => "4_9"
]
"2_3" => array:1 [
0 => "4_9"
]
"2_4" => array:1 [
0 => "4_10"
]
"4_9" => array:2 [
0 => "2_12"
1 => "2_3"
]
"4_10" => array:1 [
0 => "2_4"
]
]
How to crate this last array based on the product variations array?如何根据产品变化数组创建最后一个数组?
I created the array..... probably not the best solution...but works.我创建了数组......可能不是最好的解决方案......但可以。
Still if you got a better approach.... just post it:)不过,如果您有更好的方法....只需发布它:)
Based on the variations array:基于变量数组:
$rules = array();
foreach ($variations as $variation) {
foreach ($variation as $index => $v) {
$nextIndex = ++$index;
if(!isset($rules[$v])){
$rules[$v] = array();
if(isset($variation[$nextIndex])){
array_push($rules[$v],$variation[$nextIndex]);
}
} else {
if(isset($variation[$nextIndex])){
array_push($rules[$v],$variation[$nextIndex]);
}
}
}
$variation = array_reverse($variation);
foreach ($variation as $index => $v) {
$nextIndex = ++$index;
if(isset($variation[$nextIndex])){
array_push($rules[$v],$variation[$nextIndex]);
}
}
}
and the rules array looks like this:规则数组如下所示:
array:5 [
"2_12" => array:1 [
0 => "4_9"
]
"4_9" => array:2 [
0 => "2_12"
1 => "2_3"
]
"2_3" => array:1 [
0 => "4_9"
]
"2_4" => array:1 [
0 => "4_10"
]
"4_10" => array:1 [
0 => "2_4"
]
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.