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
DISTINCTin the SQL query? Otherwise, if you don't have control over that, you can usearray_unique()which can also be extended to have another argument indicating the sorting behavior of the resulting array.