dougan5772 2014-07-14 21:38
浏览 28
已采纳

排序结果在php数组对象中

I am doing a ticket booking website, which fetches xml results via a SOAP request in PHP. The results are then returned in the form of PHP object array. And then I display those results using PHP. In one request there are over 300 results returned. So I need to build a show by price : low to high or high to low options for the end user. I have very little experience with PHP array objects, can any one please suggest the options that I can look into for this kind of sorting?

I was thinking of storing all the results to database table then sort then using SELECT query but since there would be many searches each day and each search resulting in at-least 300 entries would make the database very bulky very soon. Is there a way to store searches temporarily inside a database?

Thank you for taking your time in reading this. I will be happy to provide any further explanation if question above is not clear to anyone.

Here is an example of my data:

Array ( 
  [0] => stdClass Object ( 
    [Id] => HS 
    [Code] => CAUMJM 
    [Status] => InstantConfirmation 
    [Price] => 87 
    [Tax] => 0 
    [SalePrice] => 0 
    [currency] => USD 
    [Type] => Guest Only 
    [guests] => Array ( 
      [0] => stdClass Object ( 
        [Category] => Standard for 1 guest 
        [number] => Array ( [0] => stdClass Object ( [Type] => A[age] => 30 ) ) 
        [totalRate] => 87 
        [ratesPerHour] => Array ( [0] => stdClass Object ( [date] => 2014-08-20 [amount] => 87 ) ) 
      ) 
    ) 
  )
)
  • 写回答

2条回答 默认 最新

  • 普通网友 2014-07-14 21:53
    关注

    Try using usort. PHP Documentation

    Supply the php array you want to sort and an anonymous function which will compare two of any of the array items and determine which item should be first in the list.

    Unless I'm wrong it seems like you are getting the soap from a 3rd party service, in which case you wouldn't be able to query the results from a database. Instead of creating your own local database cache, you could store the results of the usort function in an in-memory storage like apc or memcached. Then the computation-heavy usort would only have to run once per query. This would significantly speed up the request time for sorted results.

    The caching suggestions I made are a little off topic and not necessary to get the results you want. Look into them if you're interested, but I'll try and get you where you need to be first.

    As far as your usort implementation goes, try something like this:

        $lowToHigh = function($a, $b) {
            return  $a->SalePrice < $b->SalePrice ? 1 : -1;
        };
    
        $highToLow = function($a, $b) {
            return  $a->SalePrice < $b->SalePrice ? -1 : 1;
        };
    
        // Sort results highest to lowest
        usort($results, $highToLow);
        // Sort results lowest to highest
        usort($results, $lowToHigh);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题