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?

Qaisar Satti
32.6k18 gold badges88 silver badges138 bronze badges
asked Nov 30, 2016 at 12:45

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";
 }
}
answered Nov 30, 2016 at 13:36
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

answered Nov 30, 2016 at 13:29
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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.