0

I have an array, that I construct within a mysql fetch array loop. This array contains values such as:

Array ( [0] => 1 Hour: 1.6
        [1] => 2 Hour: 2.3
        [2] => 3 Hour: 2.8
        [3] => 4 Hour: 3.7
        [4] => 8 Hour: 7.0 )

Array ( [0] => 2 Hour: 1.5
        [1] => 4 Hour: 2.9
        [2] => 8 Hour: 3.8
        [3] => 12 Hour: 5.0
        [4] => 24 Hour: 8.9 )

What is the best way to sort the cheapest prices for a given duration, so if I select one hour, I still get the second two hours returned as the cheapest.

To further complicate this the array may sometimes contain:

Array ( [0] => Free )

I was thinking creating a class with a getPrice(duration) function, within my loop I would declare a new instance of this and add a pointer to a global array, before finally looping through each item in this global array to find the cheapest. Is this solution viable? or is there a better alternative?

while($row = mysql_fetch_array($results))
{
    $price = explode(';', $row['price']);
    print_r($price);
}

Thanks

1
  • Seems we're missing some vital info here, you show a call to explode w/ a semi-colon as the separator, then a reference to $row['price'], but there are no semi-colons or associative arrays in the data you've shared. Commented Jul 12, 2012 at 21:20

1 Answer 1

2

You should sort in the database before these values even get back to PHP, no need to do the extra work on the Webserver.

For the 'FREE' values, just use MySQL to turn them to 0 so that the ORDER BY operation works correctly.

EDIT:

Though I feel there is probly a better solution talking through and tweaking the DB paradigm here I've decided to acquiesce and provide OP w/ the type of answer he's after (w/ the minimal info provided, I still think this is probly close):

$aData = array(
        array(
            '1 Hour: 1.6',
            '2 Hour: 2.3',
            '3 Hour: 2.8',
            '4 Hour: 3.7',
            '8 Hour: 7.0'),
        array(
            '2 Hour: 1.5',
            '4 Hour: 2.9',
            '8 Hour: 3.8',
            '12 Hour: 5.0',
            '24 Hour: 8.9'),
        array(
            '2 Hour: Free',
            '4 Hour: 5.9',
            '8 Hour: 3.5',
            '12 Hour: 6.0',
            '24 Hour: 7.9'),
        );


$aCheapest = array();

// iterate all rows
foreach($aData as $aInfo) {
    // iterate row items
    foreach($aInfo as $aRow) {
        // parse the duration and rate
        list($sRawDuration, $fRate) = explode(': ', $aRow);

        $iDuration = (int)str_replace(' Hour', '', $sRawDuration);
        if(strtolower($fRate) == 'free')
            $fRate = 0.0;
        else
            $fRate = (float)trim($fRate);

        // potentially update the cheapest rate for a duration
        if(!isset($aCheapest[$iDuration]))
            $aCheapest[$iDuration] = $fRate;
        else
            $aCheapest[$iDuration] = min($aCheapest[$iDuration], $fRate);
    }
}

var_dump($aCheapest);

Output:

array(7) {
  [1]=>
  float(1.6)
  [2]=>
  float(0)
  [3]=>
  float(2.8)
  [4]=>
  float(2.9)
  [8]=>
  float(3.5)
  [12]=>
  float(5)
  [24]=>
  float(7.9)
} 
Sign up to request clarification or add additional context in comments.

7 Comments

I dont understand what you mean by sorting these values in the DB. They are stored in the DB as a text record "1 Hour: 1.6;2 Hour: 2.3..." allowing each record to have its own independent durations. This allows not creating 24+ different rows most of which are empty for most records.
When you do the SELECT statement, work in an ORDER BY clause. I'm not sure why you would put 'Hour: 2.3' into a field, seems like it would make more sense to have a float field and put in just '2.3', but either way you can strip off the 'Hour: ' part and still sort the result via ORDER BY.
That would work if in every instance we are working with a single duration. I would like to be able to select from a selection of tariffs and sort by the cheapest price. Each record has its own, non-standard durations and prices, and given a duration I would like to sort through and order by the cheapest in that instance e.g. selecting a two hour duration and having returned the price for a two hour. To do this solely in the SQL statement would I not need 24+ separate rows e.g. SELECT * from table ORDER BY duration_two_hour. My idea of implementation returns all durations and prices in...
...a single feild, reducing the data redundancy. I hope I have been clear in my explanation. Thanks.
Thanks, have modified and built upon sample code provided to achieve desired results.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.