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'], ' '; }