Hello.

Tell me how to make a check so that the elements in the information block are not duplicated every time the page is reloaded, and I would also like the information blocks to contain only those elements that are in json.

Here I get data from json and write it to an array

$url = "https://jsonplaceholder.typicode.com/users"; use Bitrix\Main\Web\HttpClient; $httpClient = new HttpClient($options); $httpClient->get($url); $res = $httpClient->getResult(); try { $ar = json_decode($httpClient->getResult(), true); } catch (\Exception $e){ echo 'Ошибка: ', $e->getMessage(), "\n"; } $json = []; foreach($ar as $item){ $json[] = [ "id" => $item['id'], "name" => $item['name'], "username" => $item['username'] ]; } 

I add to the information block elements obtained data from json

  foreach($json as $student) { CModule::IncludeModule("iblock"); $el = new CIBlockElement; $PROP = array(); $PROP[1] = $student['id']; $PROP[2] = $student['name']; $PROP[3] = $student['username']; $arLoadProductArray = Array( "MODIFIED_BY" => $USER->GetID(), "IBLOCK_SECTION_ID" => false, "IBLOCK_ID" => 1, "PROPERTY_VALUES" => $PROP, "NAME" => $student['id'], "ACTIVE" => "Y", "PREVIEW_TEXT" => "", "DETAIL_TEXT" => "" ); if($PRODUCT_ID = $el->Add($arLoadProductArray)){ echo "New ID: ".$PRODUCT_ID; } else{ echo "Error: ".$el->LAST_ERROR; } } 

So already infer the elements of the information block

  $ib = []; if(CModule::IncludeModule('iblock')) { $arSort= Array("NAME"=>"ASC"); $arSelect = Array("ID","NAME", "PROPERTY_ID", "PROPERTY_NAME", "PROPERTY_USERNAME"); $arFilter = Array("IBLOCK_ID" => 1); $res = CIBlockElement::GetList($arSort, $arFilter, false, false, $arSelect); while($ob = $res->GetNextElement()){ $arFields = $ob->GetFields(); $ib[] = [ "id" => $arFields['PROPERTY_ID_VALUE'], "name" => $arFields['PROPERTY_NAME_VALUE'], "username" => $arFields['PROPERTY_USERNAME_VALUE'] ]; } } foreach($ib as $student) { echo $student['id'], ' '; echo $student['name'], ' '; echo $student['username'], ' '; } 
  • one
    Write the student ID from json to the XML_ID of the information block element. Before adding, make a getlist on XML_ID => jsonIDs (student IDs). Add only those items that you do not have. Ie you did not find them by XML_ID - Taarim
  • And a separate tip) Do not use properties without the need. For example, the NAME of the element = the name of the student, the CODE of the element is equal to the user, and the XML_ID is equal to the identity of json. In this case, do not have to be perverted with the conclusion. This applies only if you do not make plans for these properties - Taarim

1 answer 1

Slightly reworked your code with your comments. It seems to be working)

 use Bitrix\Main\Loader; use Bitrix\Iblock\ElementTable; use Bitrix\Main\Web\Json; use Bitrix\Main\Web\HttpClient; Loader::includeModule('iblock'); $url = "https://jsonplaceholder.typicode.com/users"; $httpClient = new HttpClient($options); $httpClient->get($url); $res = $httpClient->getResult(); $iBlockId = 1; $jsonData = []; $excludedIds = []; try { $ar = Json::decode($httpClient->getResult()); } catch (\Exception $e){ echo 'Ошибка: ', $e->getMessage(), "\n"; } foreach($ar as $item){ $jsonData[$item['id']] = [ "id" => $item['id'], "name" => $item['name'], "username" => $item['username'] ]; } $testIds = array_keys($jsonData); // делаем запрос, чтобы узнать что у нас уже записано и складируем // существующие иденты в переменную для дальнейшей проверки $elementIterator = ElementTable::getList([ 'select' => [ 'ID', 'XML_ID', ], 'filter' => [ 'IBLOCK_ID' => $iBlockId, 'XML_ID' => $testIds, ] ]); while ($element = $elementIterator->fetch()) { if (in_array($element['XML_ID'], $testIds)) { $excludedIds[] = $element['XML_ID']; } } //если в исключенных идентах мы не видим текущего - добавляем новый элемент foreach ($jsonData as $student) { if (!in_array($student['id'], $excludedIds)) { $newElement = ElementTable::add([ 'MODIFIED_BY' => $USER->GetID(), 'IBLOCK_ID' => $iBlockId, 'NAME' => $student['username'], 'CODE' => $student['name'], 'XML_ID' => $student['id'], 'ACTIVE' => 'Y' ]); if (!$newElement->isSuccess()) { echo $newElement->getErrorMessages(); } else { echo $newElement->getId(); } } } //собираем и выводим $printData = ElementTable::getList([ 'select' => [ 'ID', 'NAME', 'CODE', 'XML_ID' ], 'filter' => [ 'IBLOCK_ID' => $iBlockId, ] ])->fetchAll(); foreach ($printData as $data) { echo $data['XML_ID'], ' '; echo $data['CODE'], ' '; echo $data['NAME'], ' '; }