如何找到数组的最低值?

时间:2016-02-01 04:27:05

标签: php

print_r($data['SearchAvailResponse']['Hotel'])的结果:

Array
(
    [0] => Array
        (
            [HotelNo] => 1    
            [HCode] => IDJKT_00085
            [Name] => Cebu Grand Hotel       
            [Currency] => USD
            [TotalRate] => 56          
        )

    [1] => Array
        (
            [HotelNo] => 2          
            [HCode] => IDJKT_00094
            [Name] => Best Western Plus Lex Cebu        
            [Currency] => USD
            [TotalRate] => 65       
        )

    [2] => Array
        (
            [HotelNo] => 3           
            [HCode] => IDJKT_00102
            [Name] => Best Western Sand Bar           
            [Currency] => USD
            [TotalRate] => 93
        )

    [3] => Array
        (
            [HotelNo] => 4           
            [HCode] => IDJKT_00106
            [Name] => Goldberry Suites & Hotel           
            [Currency] => USD
            [TotalRate] => 51     
        )
)

TotalRate标签是酒店价格

我想找到所有酒店的最低价格

如何找到所有酒店的最低价格?

任何帮助非常感谢

干杯

2 个答案:

答案 0 :(得分:2)

$cheapesthotel = array();
foreach($data['SearchAvailResponse']['Hotel'] as $hotel){
    if($cheapesthotel == array()){
        $cheapesthotel = $hotel;
    } elseif ($hotel['TotalRate'] < $cheapesthotel['TotalRate']){
        $cheapesthotel = $hotel;
    }
}

答案 1 :(得分:0)

function getMin($array)
{
$currentMin = 10000; 
$minObject = Array();
foreach($array as $val)
{
    if($val[totalRate] < $currentMin)
    {
        $minObject = $val;
    }


}

return minObject;
}