You should be able to step through it mentally to really see what's happening, and practicing that ought to help you debug your own code in the future.
starting with the beginning if the inner foreach loop.
1. set $i = 1 ($j is already set to 1 at this point)
2. evaluate $i == $j, which is true so enter that block
3. evaluate $count < $arraysize1, which is true, so enter that block.
4. next, go to the next pass through the inner foreach loop
5. (back at start of inner foreach loop) set $i = 2
6. evaluate $i = $j, which is false ($j still set to 1), so go to the false block, which returns 0.
ooooo. I see. Thanks a lot, man. I didn't think anyone was going to answer it.