-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_final.php
executable file
·70 lines (62 loc) · 1.9 KB
/
index_final.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
$dbCombos = array(
array(
"product_id" => 1,
"combination_name" => "<a href=\"#\">Green</a> • <a href=\"#\">128 GB</a>",
"combination_string" => "Green, 128 GB",
),
array(
"product_id" => 2,
"combination_name" => "<a href=\"#\">Green</a> • <a href=\"#\">256 GB</a>",
"combination_string" => "Green, 256 GB",
),
array(
"product_id" => 3,
"combination_name" => "<a href=\"#\">Red</a> • <a href=\"#\">128 GB</a>",
"combination_string" => "Red, 128 GB",
),
array(
"product_id" => 4,
"combination_name" => "<a href=\"#\">Red</a> • <a href=\"#\">256 GB</a>",
"combination_string" => "Red, 256 GB",
),
);
function match_product($name,$dbCombos,$id=0) {
foreach ($dbCombos as $dbItem) {
if ($id !== 0) continue;
if (strpos($name,$dbItem['combination_string']) !== false) {
$id =$dbItem['product_id'];
break;
}
}
return $id;
}
function possible_combos($dbCombos,$groups, $prefix='', $prefix2='', &$matches=[]) {
$result = array();
$group = array_shift($groups);
foreach($group as $k => $selected) {
if ($groups) {
$result = array_merge($result, possible_combos($dbCombos,$groups, $prefix . '<a href="#">'.$selected.'</a>'. ' • ', $prefix2 .$selected.', ', $matches));
} else {
$name = $prefix2.$selected;
$id = match_product($name,$dbCombos);
$result[] = [
'product_id' => in_array($id, $matches)?0:$id,
'combination_name' => $prefix . '<a href="#">'.$selected.'</a>',
'combination_string' => $name,
];
$matches[] =$id;
}
}
return $result;
}
// ------------- change variant
$dynamicCombos = [
'Color' => ['Green','Red'],
'Storage' => ['128 GB','256 GB','512 GB',],
'Memory' => ['2 GB','4 GB'],
];
echo "\n\r\n\r Case:1 / add: Memory 2GB ==============================";
$res =possible_combos($dbCombos,$dynamicCombos);
echo "\n\r";
// var_dump($data2);