P
P
photosho2021-09-14 10:20:40
1C-Bitrix
photosho, 2021-09-14 10:20:40

How to get one element with each property value?

You need to get one element from the infoblock with each value of the specified property. How can I do that? I tried to do it with "arGroupBy" (CIBlockElement::getList), but in this case the resulting array contains only the number of elements for each property value, and there are no elements in it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PetrPo, 2021-09-14
@PetrPo

Few conditions, option if the selected element is in the "group" with the highest ID

use Bitrix\Main\Loader;
use Bitrix\Iblock\ElementTable;
use Bitrix\Iblock\ElementPropertyTable;
use Bitrix\Main\Entity\ExpressionField;

Loader::includeModule('iblock');

$iblockId = 1;
$propertyId = 1;

$elementIds = [];

$iterator = ElementPropertyTable::getList([
  'select' => [new ExpressionField('ID', 'MAX(%s)', 'IBLOCK_ELEMENT_ID'), 'VALUE'],
  'filter' => ['IBLOCK_PROPERTY_ID' => $propertyId, '!VALUE' => false],
]);

while($row = $iterator->fetch()) {
  $elementIds[] = $row['ID'];
}

$elements = ElementTable::getList([
  'select' => ['ID', 'NAME'],
  'filter' => ['IBLOCK_ID' => $iblockId, 'ID' => $elementIds],
])->fetchAll();

print_r($elements);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question