Magento 1.9 – How to Formulate getCategoryIds() Statement

magento-1.9PHP

I am having trouble working with getCategoryIds() and would greatly appreciate help formulating this statement

$one = 71;
$two = 8;
$three = 4;

$categoryIds = $_product->getCategoryIds();

if(($one || $two || $three) is in $categoryIds)
{
do stuff;
}

I'm unsure of how to pull out all the categoryIds that a product might have (i.e. 5,7,2) and match against the hardcoded ones

Best Answer

You could use array_intersect (http://php.net/array_intersect), this returns you an array with values that exist in both given arrays, so you just have to count (or check if it's empty) the returned array:

$myIds = array(71, 8, 4);

$categoryIds = $_product->getCategoryIds();

$result = array_intersect($myIds, $categoryIds);

if (count($result)) {
    // do stuff;
}
Related Topic