- explode: documentation ( source)
<?php
$cars = array("A4 1.9tdi (2003-2009)",
"A4 2.0tdi (2003-2009)",
"Passat B7 1.9tdi(2003-2009)",
"Passat B7 2.0 tdi(2003-2010)");
foreach($cars as $key => $car){
$model = explode(' ', $car, 2);
$make[$model[0]][] = $car;
}
$stmt = 'Select a car: <select id="cars">';
foreach($make as $label => $type){
$stmt .= '<optgroup label="'.$label.'">';
foreach($type as $car){
list($t, $c) = explode(' ', $car, 2);
$stmt .= '<option name="'.$c.'">'.$c.'</option>';
}
$stmt .= '</optgroup>';
}
$stmt .= '</select>';
echo $stmt;
?>