# PHP find all possible solutions using recursion

I really need some ideas to get me started. Iam working on an application that finds all possible ways to swap cards.

Lets say that:

user 1 has card type A
user 2 has card type B
user 3 has card type C
And lets assume that:

user 1 wants card type B
user 2 wants card type C
user 3 wants card type A
The only solution is that:

user 1 gives user 2 his card
user 2 gives user 3 his card
user 3 gives user 1 his card
But what if there were two other users:

user 4 has card type B & wants card type C
user 5 has card type C & wants card type A
Now there is one other solution besides the one above which is that :

user 1 gives user 4 his card
user 4 gives user 5 his card
user 5 gives user 1 his card
in reality the number of users will be way more than 5 and I would have to find a way to display all possible solution. I know fthat I have to use recursion or similar algorithm to accomplish the goal. But I donâ€™t know where to start and I would really appreciate it if someone point me to the right direction.