0

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 ) ) 
      ) 
    ) 
  )
)
Andy Jones
  • 6,205
  • 4
  • 31
  • 47
Nav
  • 105
  • 1
  • 2
  • 10
  • possible duplicate of http://stackoverflow.com/questions/4282413/php-sort-array-of-objects-by-object-fields – Andy Jones Jul 14 '14 at 21:53
  • @AndyJones thanks for link to thread above but the array I am dealing with lot more complex that the one in above thread example. It has many child object inside one main object array. Something like below, can you please suggest how can same be accomplish for this? – Nav Jul 15 '14 at 20:40
  • Could you please post an example of a single result from your SOAP request? (i.e. `print_r($result[0])`) – Andy Jones Jul 15 '14 at 20:44
  • Sorry I meant to post in my last comment but it said too long, so here goes – Nav Jul 15 '14 at 20:45
  • :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 ) ) ) ) )) – Nav Jul 15 '14 at 20:45
  • And by what field would you like to sort? – Andy Jones Jul 15 '14 at 20:52

2 Answers2

0

sql order by is better option. php arrays are expensive on bigger arrays

for arrays sorting in php look these methods

http://php.net/manual/en/array.sorting.php

zod
  • 12,092
  • 24
  • 70
  • 106
0

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);
Nate
  • 51
  • 2
  • Hi @user695793, yes you are right I am getting the results from a third party. But the result is really complex object array, which I need to sort. I have not dealt with apc or memcache before, so no idea what you said there? – Nav Jul 15 '14 at 20:37
  • Check the new usort implementation I posted. – Nate Jul 16 '14 at 18:45
  • Thanks for posting the code @user695793 but I am getting sytax error with this code "Parse error: syntax error, unexpected T_LNUMBER" for the line "return $a->SalePrice < $b->SalePrice 1 : -1;" – Nav Jul 16 '14 at 19:05
  • That worked perfectly but you put the < in both functions by mistake, thank you very much for all your help :) – Nav Jul 16 '14 at 21:33