# How to find changes between two lists, with real estate items (PHP)

I'm trying to get the changes between two lists of articles in order to animate the transition between the two lists. Articles can be added, removed or moved (not swapped).

However some articles can't be moved, and in each transition all other articles should be moved below those articles.

For example, if each number represents an article id, and bold represents immovable articles, then: [1, 2, 3, 4, 5, 6] might become: [2, 4, 1, 6, 7]

I need to work out the changes required, for example in this case:

• Move 1 after 4
• Remove 5

I have been using a diff algorithm, however it doesn't understand immovable items, so it might suggest:

• Move 2 to the beginning
• Move 4 after 2
• Remove 5

I've tried various things, but can't get it to work reliably.

Moving an immovable item 3 places left is the same thing as moving 3 movable items next to it 1 place right. Use your current diff algorithm, but when it wants to move an immovable item switch to those next to it instead.

UPD. Without multiple moves per article.

Transformations:

1. Remove numbers that are not in the second list (for each item n if not in_list(final_list) then say(n removed)).
[1 2* 4* 5 6] // say("remove 3")
[1 2* 4* 6]   // say("remove 5")
2. Make an empty list size of the final list.
[_ _ _ _ _]
3. Prefill it with immovable numbers in their final positions.
[2* 4* _ _ _]
4. Loop through movable items from the first list moving them to their final positions
[2* 4* 1 _ _] // say("move 1 after 4")
[2* 4* 1 6 _] // say("move 6 after 1")