CIBlockSection::Add in a loop?

How to add all the partitions from the array? After the execution of the loop is added to the last of the under $arSectionToAdd

<?
 $arSectionToAdd = array (
'sect-1',
'sect-2',
'sect-3',
'sect-4',
);


 $bs = new CIBlockSection;


 foreach ( $arSectionToAdd as $sectionToAddd ) {


 $codeSection = str_replace(
'.',
'-',
$sectionToAddd
);


 $arFields = Array(
 'ACTIVE' = > 'Y',
 "IBLOCK_SECTION_ID" => false,
 "IBLOCK_ID" => 43,
 "NAME" => $sectionToAddd,
 "CODE" => $codeSection,
 "SORT" => '500',
 "PICTURE" => ",
 "DESCRIPTION" => ",
 "DESCRIPTION_TYPE" => "
);


 if( $ID > 0 ) {

 $res = $bs->Update(
$ID
$arFields
);
 } else {

 $ID = $bs->Add($arFields);
 $res = ( $ID > 0 );
}
}
?>
April 3rd 20 at 17:47
1 answer
April 3rd 20 at 17:49
Solution
Your code is written so that it appends the first element, and all subsequent upgrade. This happens because of the conditions if( $ID > 0 ).

$ID = $bs->Add($arFields);//add an element and write its ID into the variable
$res = ( $ID > 0 );

And in the next step without completing the zeroing of the variable ID, it updates the previous record.
Thank you very much!) - Hilma_Cummera commented on April 3rd 20 at 17:52

Find more questions by tags 1C-Bitrix