How to Iterate Over PHP Array Using While Loop?

Iterating over a PHP array is easy with the help of a while loop. We will learn how to iterate over an array using a while loop, sort the loop variables, deal with NULL elements, and so on. Let's say you have an array of integers, and you want to find out the maximum and minimum values from a specific range. 

for loop php

By SISOSISO on Dec 28, 2019
<?php
	$fruits = ["apple", "banana", "orange"];
	for($i=0;$i<count($fruits);$i++){
    echo "Index of ".$i."= ".$fruits[$i]."<br>";
    }
  ?>

Add Comment

10

php for

By Alberto PeripolliAlberto Peripolli on Apr 23, 2020
<?php
/* example 1 */

for ($i = 1; $i <= 10; $i++) {
    echo $i;
}

/* example 2 */

for ($i = 1; ; $i++) {
    if ($i > 10) {
        break;
    }
    echo $i;
}

/* example 3 */

$i = 1;
for (; ; ) {
    if ($i > 10) {
        break;
    }
    echo $i;
    $i++;
}

/* example 4 */

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>

Source: www.php.net

Add Comment

3

for loop in php

By AnkurAnkur on May 06, 2020
/*
For loop in php
*/

<?php
for ($i = 0; $i < 10; $i++) {
     echo $i."<br>";
} 
?>

Add Comment

5

php for loop

By I have no idea to make a nameI have no idea to make a name on Jul 23, 2020
for($i = 0; $i <=10; $i++){
	echo "The index is $i";
}

Add Comment

4

for php

By VasteMondeVasteMonde on Apr 17, 2021
<?php
	$fruits = ["apple", "banana", "orange"];
	for ($i = 0; $i < count($fruits); $i++) {
      	echo "Index of ".$i."= ".$fruits[$i]."<br>";
    }
	// or
	$i = 0;
	foreach ($fruits as $value) {
    	echo "Index of ".$i."= ".$value."<br>";
      	$i++;
	}
?>

Add Comment

1

for loop javascript

By Nutty NewtNutty Newt on May 09, 2020
var listItem = [
        {name: 'myName1', gender: 'male'},
        {name: 'myName2', gender: 'female'},
        {name: 'myName3', gender: 'male'},
        {name: 'myName4', gender: 'female'},
      ]

    for (const iterator of listItem) {
        console.log(iterator.name+ ' and '+ iterator.gender);
    }

Add Comment

8

You can use the While Loop condition to check which value will be included in the loop. Once it is excluded, the loop body will be finished and control will move to the next statement.

PHP answers related to "php for loop"

View All PHP queries

PHP queries related to "php for loop"

php for loop $loop laravel list $loop variable laravel custom post type query loop wordpress acf wordpress loop through and display blog posts order by date and type ?? ' ' operator in php laravel php artisan app:name in laravel 6 php artisan make:auth laravel 8 not working php artisan ui:auth laravel 7 php cache clear in laravel php extensions for laravel php laravel dump php laravel return json response php laravel rount price to 99 php run server laravel php unit testing laravel remove index.php laravel in linux server cheque print in php codeigniter install php ubuntu 20.04 php enable module php display errors php console log php sha256 php in array php uppercase each word php uppercase first letter php is numeric php now simple localhost php php include once inside a function? php if else how to convert array to string with commas in php curl php example php string to date php string functions Line : 83 -- syntax error, unexpected end of file php php file_put_contents pagination in php php print_r php check if type is mysqli_result saber value ao escolher combobox php Best Code Beautifier tools for php Uncaught TypeError: Argument 1 passed to NotORM_Result::update() php slim array shift php php PDO database connection set default php version in ubuntu php time() spread operator php Check if string starts with php radio button in php form increase PHP Memory login.php php class https//:localhost/phpmyadmin/index.php php get session variable from table Convert string to lowercase in php parseint php Php array length date to string php php reverse shell Mkdir permission denied php Php += Move uploaded file in php Php mysqli fetch assoc Php trying to get property of non-object Php strtolower date format in php array to object in php Send email php smtp hostinger Show all errors in php binemon to php password and confirm password validation in php php int to strin php timeout Php localhost str_pad_left in php php htmlspecialchars php redirect to page

Browse Other Code Languages

CodeProZone