magento getOptionId不适用于某些标签

I'm using the below code to get the option id for attribute set "size". It is returning correct id if I use option label greater than 9 (i.e, 10,12,13,14 ..)

But is not working if use option label less than 10 (i.e, 5,6,7,8,9).

$attr = 'size';
$attribute_label = 13;
$_product = Mage::getModel('catalog/product');
$attribute_name = $_product->getResource()->getAttribute($attribute_name);

if ($attr->usesSource()) {
    echo $color_id = $attribute_name->getSource()-  >getOptionId($attribute_label);
}

Example:

Output when I use option label ($attribute_label = 13) as 13 it returns 5.

Output when I use option label ($attribute_label = 6) as 6 it returns 6.

This happens because getOptionId() does not work well with numeric labels, as stated also in this question: https://magento.stackexchange.com/questions/128445/getoptionid-method-returns-invalid-option-id

What you can do is to avoid using this method and adjust your code something like:

$attribute_name = 'size';
$attribute_label = 13;
$optionId = false;
$attr = Mage::getResourceModel('catalog/product')->getAttribute($attribute_name);
if ($attr->usesSource()) {
    $options = $attr->getSource()->getAllOptions();
    foreach($options as $option) {
        if($option['label'] == $attribute_label) {
            $optionId = $option['value'];
            break;
        }
    }
}