I am coding the airplane tickets reservation system. When I receive search results from the flights database, many of the flights have the same price, but timing and some other info is different. So, I need to group all the flgiht offers by price. Sounds easy, but there is one problem when we deal with roundtrip flights (they have 2 "sectors" how we call them, one outbound, one inbound). I need to allow user to select one of the flight segments for each sector. However, if there are many available flight segments in one goup (2 or more for each flight sector) it is possible that some combination of the flight segments will not be valid, i.e. there is no flight offer with such flight segments.
Here is schematic code to illustrate this:
Code: Select all
$offers = array( array('flight_id'=>x , 'outbound'=>y, 'inbound'=>z, 'price'=n), ... );So, as you see, each offer has its id, price, and outboud/inboud flight ids. $offers array may contain up to 200 of such offers. (By the way, there is a lot more information in each offer, so code needs to be efficient).
I need to group offers by price. Each group should contain price, outbound ids list and inbound ids list. Flight id can be found later by the combination of outbound and inbound ids (it's unique). A group should not contain any combination of the outbound and inbound ids that is not found in some offer. So, it is possible that there will be several groups with the same price.
I hope you find this interesting and challenging problem to solve. I simply cannot come up with any idea.
Thanks for your help!