2016-09-18 86 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=>只是舉例來說,你可以根據需要改變輸出。 :)

相關問題