Reputation: 1
How combine this array and remove duplicate values? Thank you!
array (size=17)
0 => string 'Black' (length=5)
1 => string 'Blue' (length=4)
2 => string 'Burgundy' (length=8)
3 => string 'Glow' (length=4)
4 => string 'Granite' (length=7)
5 => string 'Green' (length=5)
6 => string 'Lime' (length=4)
7 => string 'Natural' (length=7)
8 => string 'Orange' (length=6)
9 => string 'Pink' (length=4)
10 => string 'Purple' (length=6)
11 => string 'Red' (length=3)
12 => string 'Silver' (length=6)
13 => string 'Teal' (length=4)
14 => string 'Violet' (length=6)
15 => string 'White' (length=5)
16 => string 'Yellow' (length=6)
Thank you, your code worked perfectly.
I'm still missing something because I don't get what I want.
The values (color names) are in the select form, but when a value other than 'Filter by Color' is selected, the 'id' is what pass instead of the 'text'. To make things easy, here is the complete code.
Thank you.
$Sql_product_colors = ("SELECT COUNT(p.products_id) AS id, pia.product_filter_colors FROM products p LEFT JOIN products_imprint_areas pia on p.products_id = pia.products_id
WHERE p.products_id > '1'
AND pia.product_filter_colors IS NOT NULL
GROUP BY product_filter_colors
ORDER BY product_filter_colors asc");
$sort_list_colors = array();
$product_filter_colors = tep_db_query($Sql_product_colors) or die(mysql_error());
while ($row = mysql_fetch_array($product_filter_colors)) {
$arrColor = explode(',', $row[1]);
$arrColors=array_filter(array_map('trim', $arrColor));
$sort_list_color = array_merge($sort_list_colors,$arrColors);
$sort_list_colors = array_unique($sort_list_color);
sort($sort_list_colors);
}
$sort_list_colors[0] ='Filter by Color';
$sort_list_colors[] =' '.$row[$sort_list_colors].' (' .$sort_list_colors['count'].')';
if(count($sort_list_colors)>0)
{
foreach($sort_list_colors as $id=>$text) {
$sort_range_colors[] = array('id' => $id, 'text' => $text);
}
}
// -------------------------------------- Select form
echo '<td align="center" width="25%" valign="middle"><div class="styled-select">' . tep_draw_form('colors_sort', htmlentities($_SERVER['PHP_SELF']), 'get') . '';
echo tep_draw_pull_down_menu('colors_sort', $sort_range_colors, (isset($HTTP_GET_VARS['colors_sort']) ? $HTTP_GET_VARS['colors_sort'] : ''), 'onchange="this.form.submit()"');
echo '</form></div></td>';
This is the function tep_draw_pull_down_menu. Thank you.
function tep_draw_pull_down_menu($name, $values, $default = '', $parameters = '', $required = false) {
$field = '<select name="' . tep_output_string($name) . '"';
if (tep_not_null($parameters)) $field .= ' ' . $parameters;
$field .= '>';
if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);
for ($i=0, $n=sizeof($values); $i<$n; $i++) {
$field .= '<option value="' . tep_output_string($values[$i]['id']) . '"';
if ($default == $values[$i]['id']) {
$field .= ' SELECTED';
}
$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '"', '\'' => ''', '<' => '<', '>' => '>')) . '</option>';
}
$field .= '</select>';
if ($required == true) $field .= TEXT_FIELD_REQUIRED;
return $field;
}
Upvotes: 0
Views: 121
Reputation: 26441
Use array_merge then array_unique,
$colors = array();
while ($row = mysql_fetch_array($filter_colors)) {
$arrColors = explode(',', $row[1]);
$colors = array_merge($colors,$arrColors);
}
$colors = array_unique($colors);
Warning: Please, don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Upvotes: 1