Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:52:22+00:00 2026-06-07T10:52:22+00:00

I have an array, that I construct within a mysql fetch array loop. This

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:52:24+00:00Added an answer on June 7, 2026 at 10:52 am

    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)
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one array that is filled by mysql_query. Values that are in this
I everyone i have a JSON Array that is construct like this for (var
I have an array that contains @sign in the object. I can't access that
I have an array that looks something like this: Array ( [0] => apple
I have an array that looks like this: [ Object actions: Array[2] comments: Object
I have an Array that contains some elements multiple times. Now I want to
I have this array that I am needing to insert into the database. My
I have an array that was populated by this script : var imgs =
This is sort of an odd request. I have a byte array that I
I have this API that requires me to have a specific array key to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.