There is a function to which ID $select is passed to the input, and then, at the very beginning of the function body, this number is pushed into the $nextItems . Next, in foriche, I loop through this array, substituting this ID into the query. I process the resulting records in the internal for loop and add their IDs to the same $nextItems using array_push() for subsequent recursion in foric, and after practicing the foric iteration I remove the current ID using array_shift() in the $used line to filter subsequent requests. But the ambush is that the forich works only the first time, although I’m adding new elements to the end of the first iteration.
$select = 1; $nextItems = []; array_push($nextItems, $select); $visited = ''; foreach ($nextItems as $nextItem){ $query = "SELECT * FROM items WHERE item_x = $nextItem"; for(){ ... ... array_push($nextItems, $newItem); } ... $usedArray = array_shift($nextItems); }