doumianfeng5065 2012-07-12 18:58 采纳率: 0%
浏览 9
已采纳

按数组值排序

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条回答 默认 最新

  • duanmiaosi0150 2012-07-12 19:10
    关注

    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)
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题