$v){//Loop body;}"; 3. In the loop body, each loop traverses an element and sets the variable "$con" to increase by 1. When the value of the variable "$con" is N, use the break statement Just break out of the loop, the syntax is "$con++;if($con==N){break;}"."/> $v){//Loop body;}"; 3. In the loop body, each loop traverses an element and sets the variable "$con" to increase by 1. When the value of the variable "$con" is N, use the break statement Just break out of the loop, the syntax is "$con++;if($con==N){break;}".">

Home  >  Article  >  Backend Development  >  How to loop through the first few elements of a php array

How to loop through the first few elements of a php array

青灯夜游
青灯夜游Original
2022-08-22 17:22:591987browse

Implementation steps: 1. Define a variable and assign it to 0, which is used to control the number of loop elements. The syntax is "$con=0;"; 2. Use the foreach statement to loop through the array, the syntax is "foreach($ arr as $k=>$v){//loop body;}"; 3. In the loop body, each loop traverses an element, and sets the variable "$con" to increase by 1. When the value of the variable "$con" When it is N, just use the break statement to jump out of the loop, the syntax is "$con ;if($con==N){break;}".

How to loop through the first few elements of a php array

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In the php array, you can use the foreach statement and break statement to loop through the first N elements.

Implementation steps:

Step 1: Define a variable and assign it a value of 0, which is used to control the number of loop traversals (number of elements) number).

$con=0;

Step 2: Use the foreach statement to loop through the array

foreach is a statement specially designed for traversing arrays, and is a commonly used method when traversing arrays. It provides great convenience in traversing arrays

The foreach statement traverses arrays regardless of array subscripts, and can be used for discontinuous index arrays and associative arrays with strings as subscripts.

This statement has two syntax formats:

  • Syntax format 1:

  • ##
    foreach ($array as $value){
        //循环体语句块;
    }
Traverse the given $array array , assign the value of the current array to $value in each loop.

  • Syntax format 2:

  • foreach ($array as $key => $value){
        //循环体语句块;
    }
Traverse the given $array array, and assign the value of the current array to $value, the key name is assigned to $key.

Step 3: In the loop body, set the variable $con to increment by 1. When the value of the variable $con is N, use the break statementJump out of the loop

  • Each time the loop traverses an element, add 1 to the value of variable $con

  • Then determine whether the value of variable $con is N, if so Then use the break statement to jump out of the loop

  • If not, continue to traverse the elements

  • $con++;
    if($con==N){ //循环遍历前N个元素
    	break;
    }

Complete sample code: Loop through the first 5 elements

<?php
header("Content-type:text/html;charset=utf-8"); 
$arr=[1,2,3,4,5,6,7,8,9];
var_dump($arr);
$con=0;
foreach($arr as $k=>$v){
	$res[$k]=$v;
$con++;
if($con==5){ //循环遍历前5个元素
	break;
}
}
echo "循环遍历前5个元素:";
var_dump($res);
?>

How to loop through the first few elements of a php array

Expand knowledge: Break out of the loop statement break and continue

1. Use the continue statement

The function of the continue statement is to jump out of this loop and then execute the next loop (abandon the code after the continue statement and proceed to the next loop).

Let’s take a look at the code example below.

Example 1: Output all even numbers between 1 and 10 (even numbers can be divisible by 2)

<?php
    for ($i = 1; $i <= 10; $i++) {
        if($i % 2 != 0) {
            continue;
        }
        echo $i.&#39; <br>&#39;;
    }
?>

Analysis:

  • for ($i = 1; $i The statement can loop to get all numbers between 1~10

  • if($i % 2 != 0) statement is used to judge whether the value of $i can be divided evenly by 2. If it cannot be divided evenly, the value in () is true, the continue; statement will be executed.

  • In the loop body, use the

    if($i % 2 != 0) statement to make a judgment. If the value of $i can be divided by 2, use "echo $i" output; if the value of $i cannot be divided evenly by 2, the continue; statement will be triggered, jump out of this loop (without output), and execute the next loop.

Output result:

How to loop through the first few elements of a php array

2. Use break statement

The break statement is used to jump out of the current syntax structure. It can terminate the code of the loop body and immediately jump out of the current loop and execute the code after the loop.

The difference between break and continue is that the continue statement only ends the current loop, while the break statement terminates the execution of the entire loop.

Use the code in Example 1 above and replace continue; with break;. What will be output?

<?php
    for ($i = 1; $i <= 10; $i++) {
        if($i % 2 != 0) {
            break;
        }
        echo $i.&#39; <br>&#39;;
    }
?>

The answer is:

Nothing is output. Why is this happening? Let’s analyze it:

At the beginning of the forx loop,

$i=1, is 1 less than 10? Of course, then start executing the if($i % 2 != 0) statement in the loop body;

So can 1 be divided by 2? Obviously it is not possible, so if the value in

() is true, then the "break;" statement in the if is executed, jumping out of the entire loop directly, and there is no chance to execute "echo $i.' '" statement.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to loop through the first few elements of a php array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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