0

I want to remove duplicate values from multidimensional array. I have a array output like this:-

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

I want to remove Apparel and Sports & Entertainment which is showing multiple times. I want to remove 'Product_category' repeated values. Below is my code

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

Above code is only removing the duplicate values of 'product_type'. I also want to remove the duplicate values of 'product_category' index. Thanks in advance.

Edit

I want result like this:-

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

Have a look at the front end view..

Please see the image for better understanding

junaid afzal
  • 75
  • 1
  • 2
  • 10

2 Answers2

0

Do the counting and grouping in the SQL query.

$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 ");
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • @Barmer it is still showing the `product_category` (**Apparel**) 3 times as my code is doing in asked question. Nothing is changed. Thanks. – junaid afzal Sep 18 '16 at 07:42
  • But it has a different subcategory or product type each time, doesn't it? It's the whole combination that will be unique. – Barmar Sep 18 '16 at 07:43
  • Could you show what you want the result to look like? – Barmar Sep 18 '16 at 07:45
  • Do that during output generation. Only print the category heading if it's different from the previous category. See http://stackoverflow.com/questions/27575562/how-can-i-list-has-same-id-data-with-while-loop-in-php/27575685#27575685 – Barmar Sep 18 '16 at 08:00
0

As i understand your question you want to print category once, below code will do that.

Code:

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

Output:

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

As you can see category=>Apparel and category=>Sports & Entertainment print only once.. i have added category=> for just example, you can change output as desired. :)

Noman
  • 4,088
  • 1
  • 21
  • 36