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
You can do it like this -
foreach ($attributeOptions as $option) {
if ($option['label'] == 'Alfa Romeo') {
echo "Match found";
}
else
{
echo "Match Not found";
}
}
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';
-
This is probably correct, but a bit to advanced for my basic php knowledge. I apreciate the help!Frank Archer– Frank Archer2016年11月30日 13:57:57 +00:00Commented Nov 30, 2016 at 13:57
Explore related questions
See similar questions with these tags.