【发布时间】:2019-03-19 16:49:49
【问题描述】:
当我想知道哪个变体是现有的,哪个是新的,插入或更新它时,我有一个问题。
要创建变体,我使用以下 php 函数:
<pre>
function possible_combos($groups, $prefix='') {
$result = array();
$group = array_shift($groups);
foreach($group as $selected) {
if($groups) {
$result = array_merge($result, possible_combos2($groups, $prefix .$selected.', '));
} else {
$result[] = [
'combination_string' => $prefix.$selected,
];
}
}
return $result;
}
</pre>
使用以下数组进行构建:
<pre>
Array
(
[Color] => Array
(
[0] => Green
[1] => Red
)
[Storage] => Array
(
[0] => 128 GB
[1] => 256 GB
)
)
</pre>
颜色:绿色、红色 存储:128 GB、256 GB
结果是:
<pre>
Array
(
[0] => Array
(
[combination_string] => Green, 128 GB
)
[1] => Array
(
[combination_string] => Green, 256 GB
)
[2] => Array
(
[combination_string] => Red, 128 GB
)
[3] => Array
(
[combination_string] => Red, 256 GB
)
)
</pre>
对于每个变体组合,我使用“Parent_id”创建一个新产品;
所以在编辑页面有这个:
<pre>
Array
(
[0] => Array
(
[product_id] => 1,
[combination_string] => Green, 128 GB
)
[1] => Array
(
[product_id] => 2,
[combination_string] => Green, 256 GB
)
[2] => Array
(
[product_id] => 3,
[combination_string] => Red, 128 GB
)
[3] => Array
(
[product_id] => 4,
[combination_string] => Red, 256 GB
)
)
</pre>
如果我为组合添加新属性,例如:
尺寸:大
<pre>
Array
(
[Color] => Array
(
[0] => Green
[1] => Red
)
[Storage] => Array
(
[0] => 128 GB
[1] => 256 GB
)
[Size] => Array
(
[0] => Big
)
)
</pre>
对于这种情况我想返回
<pre>
Array
(
[0] => Array
(
[product_id] => 1,
[combination_string] => Green, 128 GB, Big
)
[1] => Array
(
[product_id] => 2,
[combination_string] => Green, 256 GB, Big
)
[2] => Array
(
[product_id] => 3,
[combination_string] => Red, 128 GB, Big
)
[3] => Array
(
[product_id] => 4,
[combination_string] => Red, 256 GB, Big
)
)
</pre>
而且...如果我为属性 Storage 512 GB 添加一个新值,例如: 结果应如下所示:
<pre>
Array
(
[0] => Array
(
[product_id] => 1,
[combination_string] => Green, 128 GB, Big
)
[1] => Array
(
[product_id] => 2,
[combination_string] => Green, 256 GB, Big
)
[2] => Array
(
[combination_string] => Green, 512 GB, Big
)
[3] => Array
(
[product_id] => 3,
[combination_string] => Red, 128 GB, Big
)
[4] => Array
(
[product_id] => 4,
[combination_string] => Red, 256 GB, Big
)
[5] => Array
(
[combination_string] => Red, 512 GB, Big
)
)
</pre>
现有梳子没有 product_id。
我不知道我是否清楚这一点,但如果需要,我可以提供更多信息。
编辑 @PHPnoob
【问题讨论】:
标签: php arrays combinations