Home > Backend Development > PHP Tutorial > PHP array learning: Compare array elements and obtain the first and second largest values

PHP array learning: Compare array elements and obtain the first and second largest values

青灯夜游
Release: 2023-04-10 15:30:01
Original
3189 people have browsed it

In the previous article, we introduced the method of using the bubble algorithm to sort the array elements in ascending order. If you are interested, you can click on the link to read → "PHP Array Learning: Using the Bubble Algorithm to Sort the Elements in Ascending Order ! 》. This time we continue the PHP array learning series and introduce the method of comparing array elements and obtaining the first and second largest values.

The theme of this article is: "Given a set of numbers, calculate its maximum and second maximum values".

Implementation idea: Use an array to wrap this set of numbers, so this will involve comparing the size of the array elements

Set two variables$max and $secMax are used to store the maximum value and the second maximum value obtained. However, you need to assign initial values ​​to everyone first, assigning the first element and the second element of the array to them. But make sure $max is larger than $secMax, so you have to make a judgment

if ($arr[0] > $arr[1]){//这个if是为了防止出现数组第一个值就是最大值
        $secMax = $arr[1];
    } else if ($arr[0] < $arr[1]){
        $max = $arr[1];
    }
Copy after login

and then use a for loop to continuously traverse the $arr array. [If you don’t understand, you can read the article "PHP Loop Learning 3: How to use for loop statements to traverse arrays"]

for ($i = 0;$i< count($arr);$i++){   //循环数组
   //循环体
}
Copy after login

In the loop body, continue to $max , $secMax is compared with each value of the array ($arr[$i]):

  • if$arr[$ i] is greater than $max, then assign the $max value to $secMax, and $arr[$i]Assign to $max,

if ($arr[$i] > $max){
    $secMax = $max;
    $max = $arr[$i];
}
Copy after login
  • and if $arr[$i] is less than $ max, but $arr[$i] is greater than $secMax, then only $secMax and $arr[$i] are exchanged , copy $arr[$i] to $secMax.

if ($arr[$i] < $max && $arr[$i] > $secMax){
      $secMax = $arr[$i];
}
Copy after login

By continuing to compare like this, you can ensure that $max stores the maximum value of the array, and $secMax stores the second largest value of the array.

Let’s take a look at the complete implementation code:

function takeNum($arr){
	//定义了两个变量$max和$secMax来存放最大和第二大的数值
    $max = $arr[0];
    $secMax = $arr[0];
    if ($arr[0] > $arr[1]){//这个if是为了防止出现数组第一个值就是最大值
        $secMax = $arr[1];
    } else if ($arr[0] < $arr[1]){
        $max = $arr[1];
    }
    for ($i = 0;$i< count($arr);$i++){//循环数组
        if ($arr[$i] > $max){
            $secMax = $max;
            $max = $arr[$i];
        } else if ($arr[$i] < $max && $arr[$i] > $secMax){
            $secMax = $arr[$i];
        }
    }
    echo "最大值为".$max.",第二大值为".$secMax;
}
Copy after login

Test it: define the following array, call the takeNum($arr) function to see the output

$arr = array(555,5,1,429,18,21,52,2,52,9,3,);
takeNum($arr);
Copy after login

It can be seen that the first largest value is 555 and the second largest value is 429; see if the output result is this:

PHP array learning: Compare array elements and obtain the first and second largest values

OK, the output result is correct .

Next let’s talk about the knowledge points used. There is no need to say more about loops (you can read the article "PHP Loop Learning Series Summary"), let’s give you a brief introduction if else statement.

The if else statement is a type of execution based on conditional judgment in process control. When this statement is executed, the condition is first judged, and then the corresponding operation is made based on the judgment result. It can be subdivided into three types, namely if statement, if...else statement, if...else if...else statement.

The syntax of if statement:

if (判断条件) {
    语句块;
}
Copy after login

if...else The syntax of statement:

if (判断条件) {
    语句块 1;
} else {
    语句块 2;
}
Copy after login

if...else The syntax of if...else statement:

if (判断条件 1) {
    语句块 1;
} else if (判断条件 2) {
    语句块 2;
} else if (判断条件 3) {
    语句块 3;
}
......
Copy after login

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays: PHP function array array function video explanation, come and learn!

The above is the detailed content of PHP array learning: Compare array elements and obtain the first and second largest values. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template