Home >Backend Development >PHP Tutorial >How to match transactions in php?
A user spent 2 yuan to buy 100 bottles of water today; the market price is now 2 yuan; and then placed an order: wait for the market price to rise to 2.5 yuan to sell 100 bottles. Just wait for people to buy it
B user: The current market price is 3 yuan; then place an order to buy 50 bottles for 2.5 yuan.
How to use PHP queue to enable user B to automatically buy 50 bottles of user A. B’s transaction is completed. Because A's transaction has not been sold out, the order continues to be placed; how to implement this kind of transaction for N users?
It’s easy to understand if you understand the above example as stock pending order.
A user spent 2 yuan to buy 100 bottles of water today; the market price is now 2 yuan; and then placed an order: wait for the market price to rise to 2.5 yuan to sell 100 bottles. Just wait for people to buy it
B user: The current market price is 3 yuan; then place an order to buy 50 bottles for 2.5 yuan.
How to use PHP queue to enable user B to automatically buy 50 bottles of user A. B’s transaction is completed. Because A's transaction has not been sold out, the order continues to be placed; how to implement this kind of transaction for N users?
It’s easy to understand if you understand the above example as stock pending order.
This question has nothing to do with language.
Two queues are needed, one to store "buy" and one to store "sell". The queues are sorted by price, and the same price is sorted by time.
Then the program continuously compares the highest buying price and the lowest selling price. If there is an overlap between the two, a transaction will be made, and the transaction price will be calculated based on the selling price.