2016-09-18 95 views
0

我想从多维数组中删除重复的值。我有一个阵列输出是这样的: -如何从PHP中的数组中删除重复值?

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_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

+0

只需从'SELECT'中删除'product_category'查询 – Noman

+0

@Noman但我想'product_category'只显示一次。如果我删除了'product_category',那么它将如何显示。感谢您的回复。 –

+0

您需要在呈现产品类别时设置条件。这是最简单的解决方案:) – Noman

回答

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 "); 
+0

@Barmer它仍显示'product_category' (**服装**)3次,因为我的代码是在问问题中做的。没有什么改变。谢谢。 –

+0

但它每次都有不同的子类别或产品类型,不是吗?整个组合将是独一无二的。 – Barmar

+0

你能展示你想要的结果吗? – Barmar

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=>只是举例来说,你可以根据需要改变输出。 :)