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.

3
  • 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. Commented Mar 8, 2019 at 15:18
  • 1
    looks like you should filter with the store_id Commented Mar 8, 2019 at 19:37
  • Use in_array to remove duplicates inside foreach.. Commented Mar 9, 2019 at 8:30

2 Answers 2

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); 
0
0

array_unique function only works with one-dimensional arrays. Since you have a multidimensional array, you'll need to iterate over the array and create a new array with the unique values.

$originalArray = [ /* your array here */ ]; $uniqueArray = []; foreach ($originalArray as $value) { $serializedValue = serialize($value); if (!in_array($serializedValue, $uniqueArray)) { $uniqueArray[] = $serializedValue; } } // Convert back to the normal array $uniqueArray = array_map('unserialize', $uniqueArray); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.