如何从PHP中删除数组中的重复值?

时间:2016-09-18 07:06:24

标签: php arrays

我想从多维数组中删除重复值。我有这样的数组输出: -

0 => 
    array (size=4)
      'product_category' => string 'Apparel' (length=7)
      'product_sub_cat' => string 'T-Shirts' (length=8)
      'product_type' => string 'T-Shirts' (length=8)
      'count' => int 14
  1 => 
    array (size=4)
      'product_category' => string 'Apparel' (length=7)
      'product_sub_cat' => string 'Hoodies & Sweatshirts' (length=21)
      'product_type' => string 'Hoodies & Sweatshirts' (length=21)
      'count' => int 5
  2 => 
    array (size=4)
      'product_category' => string 'Apparel' (length=7)
      'product_sub_cat' => string 'Sweaters' (length=8)
      'product_type' => string 'Sweaters' (length=8)
      'count' => int 1
  3 => 
    array (size=4)
      'product_category' => string 'Sports & Entertainment' (length=22)
      'product_sub_cat' => string 'Team Sports' (length=11)
      'product_type' => string 'Basketball' (length=10)
      'count' => int 1
  4 => 
    array (size=4)
      'product_category' => string 'Sports & Entertainment' (length=22)
      'product_sub_cat' => string 'Other Sports & Entertainment Products' (length=37)
      'product_type' => string 'Other Sports & Entertainment Products' (length=37)
      'count' => int 1

我想删除服饰和运动&娱乐,多次展示。我想删除" Product_category' 重复的值。以下是我的代码

$stmt = $pdo->prepare("SELECT `product_category`, `product_sub_cat`, `product_type` FROM `search` WHERE product_name LIKE {$string} 
                OR `product_type` like '%" . $keyword . "%' OR `product_sub_cat` like '%" . $keyword . "%' ORDER BY `product_type` 
                LIKE '%" . $keyword . "%' DESC, `product_name`LIKE '%" . $keyword . "%' DESC ");
$stmt->execute();
$RelatedCategoryProduct = $stmt->fetchAll(PDO::FETCH_ASSOC);

//------------------------------- Count and Remove Duplicate Related Category Array values ------------------------------

function unserialize_unique_count($input, $k = 'count') {
    $a = [];
    foreach ($input as $d) {
        $s = serialize($d);
        $a[$s] = (isset($a[$s]) ? ($a[$s] + 1) : 1);
    }
    foreach ($a as $s => $c) {
        $a[$s] = unserialize($s) + [ $k => $c];
    }
    return array_values($a);
}

$grouped_with_count = unserialize_unique_count($RelatedCategoryProduct);

以上代码仅删除' product_type' 的重复值。我还想删除' product_category' 索引的重复值。提前谢谢。

修改

我想要这样的结果: -

0 => 
    array (size=4)
      'product_category' => string 'Apparel' (length=7)
      'product_sub_cat' => string 'T-Shirts' (length=8)
      'product_type' => string 'T-Shirts' (length=8)
      'count' => int 14
  1 => 
    array (size=4)

      'product_sub_cat' => string 'Hoodies & Sweatshirts' (length=21)
      'product_type' => string 'Hoodies & Sweatshirts' (length=21)
      'count' => int 5
  2 => 
    array (size=4)

      'product_sub_cat' => string 'Sweaters' (length=8)
      'product_type' => string 'Sweaters' (length=8)
      'count' => int 1
  3 => 
    array (size=4)
      'product_category' => string 'Sports & Entertainment' (length=22)
      'product_sub_cat' => string 'Team Sports' (length=11)
      'product_type' => string 'Basketball' (length=10)
      'count' => int 1
  4 => 
    array (size=4)

      'product_sub_cat' => string 'Other Sports & Entertainment Products' (length=37)
      'product_type' => string 'Other Sports & Entertainment Products' (length=37)
      'count' => int 1

看一下前端视图..

Please see the image for better understanding

2 个答案:

答案 0 :(得分:0)

在SQL查询中进行计数和分组。

$stmt = $pdo->prepare("
    SELECT `product_category`, `product_sub_cat`, `product_type`, COUNT(*) as count
    FROM `search` 
    WHERE product_name LIKE {$string} 
        OR `product_type` like '%" . $keyword . "%' OR `product_sub_cat` like '%" . $keyword . "%' 
    GROUP BY `product_category`, `product_sub_cat`, `product_type`
    ORDER BY `product_type`  LIKE '%" . $keyword . "%' DESC, `product_name`LIKE '%" . $keyword . "%' DESC ");

答案 1 :(得分:0)

据我了解你的问题,你想打印一次类别,下面的代码就可以了。

代码:

$category = [];
$column   = 'product_category';
foreach($array as $subarray) {
    $i = 0;
    foreach($subarray as $key => $val) {
        if( $key == $column && ! in_array($val, $category) ) {
            $category[] = $val;
            echo "<h2>".$val."</h2>";
        }
        else if( $key != $column ) {
            echo '<br>'.$key.' : <b>'.$val.'</b>';
        }
    }
    echo '<br>';
}

输出:

category=>Apparel


product_sub_cat : T-Shirts
product_type : T-Shirts
count : 14

product_sub_cat : Hoodies & Sweatshirts
product_type : Hoodies & Sweatshirts
count : 5

product_sub_cat : Sweaters
product_type : Sweaters
count : 1
category=>Sports & Entertainment


product_sub_cat : Team Sports
product_type : Basketball
count : 1

product_sub_cat : Other Sports & Entertainment Products
product_type : Other Sports & Entertainment Products
count : 1

正如您所看到的那样category=>Apparelcategory=>Sports & Entertainment仅打印一次..我已添加category=>作为示例,您可以根据需要更改输出。 :)