合并所有值并删除重复项

时间:2015-03-12 04:06:34

标签: php arrays

如何组合此数组并删除重复值? 谢谢!

  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)

谢谢,您的代码完美无缺。

我仍然缺少一些东西,因为我没有得到我想要的东西。

值(颜色名称)采用选择形式,但是当选择“按颜色过滤”以外的值时,“id”是通过而不是“文本”。为了简单起见,这是完整的代码。

谢谢。

$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>';

这是函数tep_draw_pull_down_menu。谢谢。

 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('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
    }
    $field .= '</select>';
    if ($required == true) $field .= TEXT_FIELD_REQUIRED;
    return $field;
  }

1 个答案:

答案 0 :(得分:1)

使用array_merge然后array_unique

$colors = array();
while ($row = mysql_fetch_array($filter_colors)) {
    $arrColors = explode(',', $row[1]);
    $colors = array_merge($colors,$arrColors);
}
$colors = array_unique($colors);

警告: Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial