0

In Magento i have an Array with products and from those products i want to have the categories where they are in. That's what i have, but I'm using a foreach to get through the products, so there are duplicates that need to be removed.

I have the category names already with the foreach, but there are some duplicates now that need to be removed.

<?php foreach ($_productCollection as $_product): ?>
    <div class="bk-all-products">
        <?php
            $bk_product_id = $_product->getCategoryIds(); 
            $bk_category_id = $bk_product_id[1];

            $categoryId = $bk_category_id;
            $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $category = $_objectManager->create('Magento\Catalog\Model\Category')
            ->load($categoryId);
            $bk_category_id_name = $category->getName();

            echo $bk_category_id_name;

            echo "<br><br>";
        ?>
    </div>
<?php endforeach; ?>

Additional information

This is what is returns when I also print the Array in the foreach:

Array ( [0] => 354 [1] => 362 [2] => 360 [3] => 414 ) Cafeïnevrije koffie

Array ( [0] => 354 [1] => 362 [2] => 364 [3] => 414 ) Cafeïnevrije koffie

Array ( [0] => 354 [1] => 367 ) Koffiepakketten

Array ( [0] => 354 [1] => 364 ) Filterkoffie

Array ( [0] => 354 [1] => 360 ) Espressokoffie

Array ( [0] => 354 [1] => 360 [2] => 414 ) Espressokoffie

Array ( [0] => 354 [1] => 364 [2] => 414 ) Filterkoffie

Array ( [0] => 354 [1] => 360 [2] => 414 ) Espressokoffie

Array ( [0] => 354 [1] => 367 ) Koffiepakketten

Array ( [0] => 354 [1] => 367 ) Koffiepakketten

Array ( [0] => 367 [1] => 354 ) Koffiebonen

Array ( [0] => 367 [1] => 354 ) Koffiebonen

6
  • Use an array to store the outputted ones in, befoure outputting one, test if it's in the array Commented Jul 30, 2019 at 11:40
  • Duplicate products or duplicate categories across products in the collection? Commented Jul 30, 2019 at 11:41
  • 1
    Do it in the sql query maybe it's better by grouping similar products or categories Commented Jul 30, 2019 at 11:42
  • Why not select DISTINCT in the SQL query? Otherwise, if you don't have control over that, you can use array_unique() which can also be extended to have another argument indicating the sorting behavior of the resulting array. Commented Jul 30, 2019 at 11:47
  • @DarkBee How can I store the output from the $bk_product_id[1]; in an Array and loop through them to remove the duplicates? Commented Jul 30, 2019 at 11:48

1 Answer 1

2

The easy way would be to store the already displayed categories in an Array and check if the one you want to display is not already in it using in_array

EDIT: probably better to store the id than the name, as you can avoid getting the name if already fetched:

<?php
$diplayed_categories = []; //initializing array
foreach ($_productCollection as $_product):
?>
    <div class="bk-all-products">
        <?php
            $bk_product_id = $_product->getCategoryIds(); 
            $bk_category_id = $bk_product_id[1];

            if(!in_array($bk_category_id, $diplayed_categories)){ //testing if not in array
                $diplayed_categories[] = $bk_category_id; //filling the array

                //moved inside the if, no need to fetch it again if it exists
                //$categoryId = $bk_category_id; //useless var
                $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $category = $_objectManager->create('Magento\Catalog\Model\Category')
                  ->load($bk_category_id); //replaced by $bk_category_id
                $bk_category_id_name = $category->getName();

                echo $bk_category_id_name;

                echo "<br><br>";
            }
        ?>
    </div>
<?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.