1

I am trying to get attribute option values for specific products.

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $baseUrl = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore(0)->getBaseUrl();
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $product1 = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
        $op_array = array();
        $val_array = array();
        $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($product1->getId());
        $_childProducts = $_product->getTypeInstance()->getUsedProducts($_product);
        foreach ($_childProducts as $simpleProduct){

            // echo '<pre>' , var_dump($simpleProduct->getData()) , '</pre>';
            $sku = $simpleProduct->getSku();
           // echo "<br/>";
            $stringSku = '"' . $sku . '"';
            $sql = "select * from inventory_stock_4 where sku =". $stringSku;
            $result = $connection->fetchAll($sql);
            //var_dump($result);
            //echo $option_label = $result[0]['quantity'];
            //echo "<br/>";
            $tableName = $resource->getTableName('eav_attribute_option_value');


            $sql2 = "select distinct * FROM " . $tableName . " where option_id=".$simpleProduct['jasani_size'];
            $result2 = $connection->fetchAll($sql2);
            $option_label = $result2[0]['value'];
            foreach($result2 as $size){

               var_dump($size);
    }

I get an array like this

         array(4) { ["value_id"]=> string(3) "611" 
    ["option_id"]=> string(2) "25" ["store_id"]=> string(1) "1" 
["value"]=> string(2) "XS" }
         array(4) { ["value_id"]=> string(3) "612"
     ["option_id"]=> string(2) "25" ["store_id"]=> string(1) "0" 
["value"]=> string(2) "XS" } 
        array(4) { ["value_id"]=> string(3) "613" 
    ["option_id"]=> string(2) "25" ["store_id"]=> string(1) "3" 
["value"]=> string(2) "XS" }
         array(4) { ["value_id"]=> string(3) "614" 
    ["option_id"]=> string(2) "25" ["store_id"]=> string(1) "4"
 ["value"]=> string(2) "XS" }
         array(4) { ["value_id"]=> string(3) "619" 
    ["option_id"]=> string(2) "27" ["store_id"]=> string(1) "1"
 ["value"]=> string(1) "M" } 
        array(4) { ["value_id"]=> string(3) "620" 
    ["option_id"]=> string(2) "27" ["store_id"]=> string(1) "0" 
["value"]=> string(1) "M" } 
        array(4) { ["value_id"]=> string(3) "621" 
    ["option_id"]=> string(2) "27" ["store_id"]=> string(1) "3" 
["value"]=> string(1) "M" }
         array(4) { ["value_id"]=> string(3) "622"
     ["option_id"]=> string(2) "27" ["store_id"]=> string(1) "4"
 ["value"]=> string(1) "M" } 

I get this repeated values like XS and M I can't filter unique vales in this. I also tried array_unique(), array_filter() nut no use.

  • I'm not sure, what you are trying to do that way, but if you set $option_label = [] before the first foreach and replace $option_label = $result2[0]['value']; with $option_label[$result2[0]['value']] = $result2[0]['value']; you will have an array of unique labels. – HelgeB Mar 8 at 15:18
  • 1
    looks like you should filter with the store_id – Herve Tribouilloy Mar 8 at 19:37
  • Use in_array to remove duplicates inside foreach.. – aravind Mar 9 at 8:30
1

array_unique is not working on multi-dimensionnal array, see the doc here :

array_unique() is not intended to work on multi dimensional arrays.

To filter an multi-dimensionnal try :

$temp = array_unique(array_column($array, 'value'));
$unique_arr = array_intersect_key($array, $temp);

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.