Temporarily store the slot id in a variable and also the details of the item you grabbed, in a separate array, then when the item is dropped onto an existing item, use the slot id of the slot where you are dropping and set all of that item's details to at stored id. Then for the item being placed use the dropped slot id to set the stored details.
So for example, you picked up item at 6 and plan to drop it onto 2. Store in a variable '6'(itemid) and everything at 6,0 6,1 6,2 etc will be stored in a row of the new array. When the item is dropped onto slot 2, set var id(6),0 6,1 6,2 etc to the details of itemarray at dropped slot id(2),0 2,1 2,2 etc. Then set the dropped slot id(2),0 2,1 2,2 in item array to the corresponding values from the new array.
Pick up item 6 - > store 6 in var and inv.at(6,y) in secondarray.at(0,y)
Drop on item 2 - > set inv.at(6,y) to values inv.at(2,y) (swaps 2 to 6)
set inv.at(2,y) to values secondarray.at(0,y) (swaps 6 to 2)