1

I have an array of makes and values printed as following:

$name='makemagento';
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem();
$attributeId = $attributeInfo->getAttributeId();
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions(false); 
print_r($attributeOptions);

array($attributeOptions);

This gives me a list of results like this:

Array
(
[0] => Array
    (
        [value] => 1035
        [label] => Alfa Romeo
    )


)

I am trying to check if a value exists.

I do the following:

if (in_array('Alfa Romeo', $attributeOptions['label']))
 {
 echo "Match found";
 }
 else
 {
 echo "Match not found";
 }

But i can't seem to successfully get a "match found" result from this.

Can anybody advise?

2 Answers 2

0

You can do it like this -

foreach ($attributeOptions as $option) {
   if ($option['label'] == 'Alfa Romeo') {
      echo "Match found";
   }
   else
   {
     echo "Match Not found";
   }
}
0
2

in_array() does not work on multidimensional arrays. You could write a recursive function to do that for you:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

Usage:

echo in_array_r("Alfa Romeo", $attributeOptions) ? 'found' : 'not found';

Reference

1
  • This is probably correct, but a bit to advanced for my basic php knowledge. I apreciate the help! Commented Nov 30, 2016 at 13:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.