<?php
$attribute_options_string = "Spice (Very Hot/3, Hot/2, Medium/1) | Size(Small,Medium,Large)";
$attribute_options_array = array();
$attributes = preg_split('/\s*\|\s*/', $attribute_options_string);
foreach ($attributes as $attribute) {
preg_match('/\s*(\w+)\s*\(([^)]+)/', $attribute, $matches);
$attribute_name = strtolower($matches[1]);
$options = preg_split('/\s*,\s*/', $matches[2]);
foreach ($options as $option) {
if (strpos($option, '/') !== false) {
list($option_name, $option_value) = preg_split('#\s*/\s*#', $option);
$attribute_options_array[$attribute_name][strtolower($option_name)] = $option_value;
}
else {
$attribute_options_array[$attribute_name][] = strtolower($option);
}
}
}
var_export($attribute_options_array);
- Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- array (
'spice' =>
array (
'very hot' => '3',
'hot' => '2',
'medium' => '1',
),
'size' =>
array (
0 => 'small',
1 => 'medium',
2 => 'large',
),
)
preferences:
164.9 ms | 408 KiB | 5 Q