For nesting, check out concatenation - if you do num##ind,
foreach (num, list) {
foreach (num2, list2) {
// ...
}
}
would get their own vars.
For getting the next value, that’s how it goes (and also you should account for zero-sized arrays)