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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里