The placement of the line where you define the object in the $row variable, needs to be placed inside the foreach loops.
Otherwise you keep overwriting the same object for each iteration.
I attached an updated version of your script. Try that one
The placement of the line where you define the object in the $row variable, needs to be placed inside the foreach loops.
Otherwise you keep overwriting the same object for each iteration.
I attached an updated version of your script. Try that one