Example my array
$options = array(
array(\"brand\" => \"Puma\",\"code\" => \"p01\",\"name\" => \"Puma One\"),
array(\"brand\" => \"Puma\"
You can create another array keyed by the brand:
$newOptions = array();
foreach ($options as $option) {
$brand = $option['brand'];
$code = $option['code'];
$name = $option['name'];
$newOptions[$brand][$code] = $name;
}
This will produce an array like this:
$newOptions = array(
'Puma' => array('p01' => 'Puma One', 'p02' => 'Puma Two'),
'Nike' => array('n01' => 'Nike One', 'n02' => 'Nike Two'),
...
);
If you can directly format your array like this, you can skip the first step.
Then iterate over this new array and output the options:
foreach ($newOptions as $brand => $list) {
echo "<optgroup label=\"$brand\">\n";
foreach ($list as $code => $name)
echo "<option value=\"$code\">$name</option>\n";
echo "</optgroup>\n";
}