Reputation:
I have results in a variable like this:
string(4) "iFit" string(5) "Žene" string(6) "Odeća" string(5) "Brend" string(7) "Trening" string(5) "Šorc" string(16) "Reebok Les Mills" string(9) "Les Mills" string(6) "Outlet" string(6) "Odeća" string(4) "iFit" string(5) "Žene" string(6) "Odeća" string(5) "Brend" string(6) "Reebok" string(7) "Trening" string(6) "Majica" string(6) "Outlet" string(4) "Yoga" string(6) "Odeća" string(6) "Odeća" string(4) "iFit" string(5) "Žene" string(6) "Odeća" string(5) "Brend" string(6) "Reebok" string(7) "Trening" string(6) "Majica" string(6) "Outlet" string(4) "Yoga" string(6) "Odeća" string(6) "Odeća" string(4) "iFit" string(5) "Žene" string(6) "Odeća" string(5) "Brend" string(6) "Reebok" string(7) "Trening" string(6) "Majica" string(6) "Outlet" string(4) "Yoga" string(6) "Odeća" string(6) "Odeća" string(4) "iFit" string(5) "Žene" string(6) "Odeća" string(5) "Brend" string(6) "Reebok" string(7) "Trening" string(6) "Majica" string(6) "Outlet" string(4) "Yoga" string(6) "Odeća" string(6) "Odeća" string(4) "iFit" string(5) "Žene" string(6) "Odeća"
How can I display only unique values? No matter what I do I get the sam result
What i did so far:
$arr = explode( " " , $string );
$arr = array_unique( $arr );
$string = implode(" " , $arr);
No matter what I do I always get the same result. But I need to put those unique values in the dropdown. Please help me find the solution.
This is my full code:
foreach ($collection as $product){
$categoryIds = $product->getCategoryIds();
$categories = $categoryCollection->create()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', $categoryIds);
foreach ($categories as $category) {
var_dump($category->getName());
}
}
I am trying to get all categories in magento and put them down in frontend dropdown. Here is a foreach that I need to get all categories names but I get the result as shown above.
Upvotes: 2
Views: 1366
Reputation: 111
If array_unique()
is not working properly for whatever reason, try using this:
$string;
$arr = explode( " " , $string );
$result = array();
foreach($arr as $key=>$val) {
$result[$val] = true;
}
In most cases this is even faster than using array_unique()
. Hope this helped.
You can learn more from the PHP documentation.
Upvotes: 1