簡體   English   中英

從數組中獲取最大的數字

[英]Get the highest number from an array

我有以下數組:

Array ( [0] => 80 ) Array ( [0] => 20 ) Array ( [0] => 90 )

碼:

    $percentage_query        = $this->Common_model->getTableData('prop_payment_percentage', array('list_id' => $id));

    $percentage_result = $percentage_query->result_array();

        foreach ($percentage_result as $ptime) {

            $percentage_start_date = $percentage_query->row()->start_date;
            $percentage_end_date = $percentage_query->row()->end_date;
            $percentage_percentage = $percentage_query->row()->percentage;

            //Days between start date and end date -> seasonal price
            $start_time = $ptime['start_date'];
            $end_time = $ptime['end_date'];
            $hightest_percentage = $ptime['percentage']; // this is the array 802090

            //help me echo 90
        }

我該如何退還Value 90? 因為這是我想返回到$ variable的最高數字

除非對數組進行排序,否則這將是您最好的選擇。 如果已排序,則僅取第一個和最后一個元素。

當然,如果不進行排序,那么與僅循環瀏覽一次相比,保證先進行排序並搶先進行和最后進行的效率較低。 即使是最好的排序算法也必須多次查看每個元素(每個元素平均O(log N)次。總計為O(N * Log N)。一次簡單的掃描只有O(N)。

如果要快速訪問數據結構中最大的元素,請查看堆,以一種有效的方式將對象保持某種順序。

sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key

如果我們使用升序,則要訪問最后一個元素,我們必須計算數組大小,而arr [last_position]給出最高順序

但是通過以降序對數組進行排序,就無需計算數組大小。 只需訪問第一個號碼,您將獲得最高號碼

如果您有一個包含數組的數組,則可以這樣做:

$masterArray = array(
  array(80),
  array(20),
  array(90),
);
$highestValue = 0;
foreach($masterArray as $arr){
  $highestValue = $arr[0] > $highestValue ? $arr[0] : $highestValue;
}
echo $highestValue; //echo 90;

這是示例,如果您有3個數組或變量。

$a1 = Array ( "0" => 80 );
$a2 = Array ( "0" => 20 );
$a3 = Array ( "0" => 90 );

$a4 = array_merge($a1,$a2);

$a5 = array_merge($a4,$a3);
echo '<pre>';
print_r(max($a5));

您不需要循環。 合並三個數組,然后使用max();

    $array = array_merge(array(80),array(20),array(20));
    $max = max($array);
<?
$a=array(0 => 1300 );
$b=array(0 => 500 );
$c=array(0 => 1220 );

$MaxValue=0;

if($a[0]>$b[0])
{
        $MaxValue=$a[0];
    if($b[0]>$MaxValue)
    {
        $MaxValue=$b[0];
    }
    else
    {
        if($c[0]>$MaxValue)
        {$MaxValue=$c[0];}
    }
}
else
{
    if($b[0]>$c[0])
    {
        $MaxValue=$b[0];
    }
    else
    {
        $MaxValue=$c[0];
    }
}
echo $MaxValue;
?>

最簡單的方法是使用php中的標准代碼功能:

$array = array(0 => 80, 1 => 20, 2 => 90);
$variable = max($array);

另請參見http://php.net/manual/en/function.max.php中的文檔

更新:此函數不僅需要整數數組。 所有這些均有效,並按預期返回3

$a1 = array(1);
$a2 = array(2);
$a3 = array(3);

$i1 = 1;
$i2 = 2;
$i3 = 3;

$arr1 = array($a1, $a2, $a3);
$arr2 = array($i1, $i2, $i3);

echo max($a1, $a2, $a3), "\n";  // 3
echo max($i1, $i2, $i3), "\n";  // 3
echo max($arr1), "\n";  // 3
echo max($arr2), "\n";  // 3

數組中使用什么鍵都沒有關系。

更新2:如果您確實需要使用值802090並且可以確保該值始終為2位數百分比,則還可以使用str_split函數(然后再次使用max函數):

$hightest_percentage = 802090;
$hpr = str_split($hightest_percentage, 2);  // array ( 80, 20, 90 );
print_r(max($hpr)); // 90

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM