| <?php |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function setAttr(array $attr_group, int $index = 0, array $keys = [], array $attrs = []) |
| { |
| static $row = 0; |
| |
| if ($index == 0) { |
| foreach($attr_group as $i) { |
| $keys[] = 0; |
| } |
| } |
| |
| if (isset($attr_group[$index])) { |
| $attr_values = explode(',', $attr_group[$index]['attr_values']); |
| foreach($attr_values as $key => $attr_value) { |
| $keys[$index] = $key; |
| if ($index + 1 == count($attr_group)) { |
| foreach($keys as $i => $item) { |
| $attrs[$row][$attr_group[$i]['name']] = explode(',', $attr_group[$i]['attr_values'])[$item]; |
| } |
| $attrs[$row]['price'] = '0'; |
| $row++; |
| } else { |
| $attrs = setAttr($attr_group, $index + 1, $keys, $attrs); |
| } |
| } |
| } |
| |
| return $attrs; |
| } |
| |
| |
| $attr_group = [ |
| [ |
| 'name' => '颜色', |
| 'attr_values' => '白色,红色' |
| ], |
| [ |
| 'name' => '内存', |
| 'attr_values' => '64G,128' |
| ] |
| ]; |
| $attrs = setAttr($attr_group); |
| var_dump($attrs); |
| |
| ?> |