Home > Backend Development > PHP Tutorial > How to match transactions in php?

How to match transactions in php?

WBOY
Release: 2016-08-04 09:19:38
Original
2203 people have browsed it

Give me a scene


  • 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.

Question

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?

Instructions

  • It’s easy to understand if you understand the above example as stock pending order.

Reply content:

Give me a scene


  • 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.

Question

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?

Instructions

  • 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.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template