Hello. There is a table of the form: 
It consists of two tables - the structure of the table t1:
table structure t2:
Relationship: t1.id = t2.value_id
In accordance with the 'uf_sap_code' and 'uf_sap_code_parent' fields, you need to find the parent id and fill in the 'iblock_section_id' field with them. I'm trying to write recursion, but I can't:
function getCategory() { global $ibs; $query = $ibs->GetList( array('sort' => 'asc'), array('IBLOCK_ID' => 5), false, array('ID', 'NAME', 'IBLOCK_SECTION_ID', 'UF_SAP_CODE', 'UF_SAP_CODE_PARENT') ); $result = array(); while ($row = $query->Fetch()) { $result[$row["UF_SAP_CODE_PARENT"]][] = $row; } return $result; } $category_arr = getCategory(); function getParentId($parent_id) { global $category_arr; if (isset($category_arr[$parent_id])) { foreach ($category_arr[$parent_id] as $value) { $id = $value["ID"]; getParentId($value["UF_SAP_CODE"]); } return $id; } } Unfortunately, recursion was never encountered before. As I understand it, you need the condition of deepening, but I can not figure out. Tell me please.

