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?

0

You can do it like this -

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

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

  • This is probably correct, but a bit to advanced for my basic php knowledge. I apreciate the help! – Frank Archer Nov 30 '16 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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