PHP loop array

To access elements of an array, you can use for loop, foreach loop, or while loop. for loop and while loop can be used to assign values to and read array elements. With foreach loop you can only read array elements. However, when using foreach loop to read array elements, you do not need to know the number of elements of the array.
Example:

<?php

$Arr=array(); //define an array of 10 element
//using for loop to assign values to array elements
for($i=0;$i<10;$i++)
$Arr[$i]=$i;
//using foreach loop to print all array elements
foreach($Arr as $elem)
echo $elem.'<br>';
//using while loop to print the results of multiplying array elements by 10
$j=0;
while($j<count($Arr)){

echo $Arr[$j]*10;
echo "<br>";
$j++;
}

?>

Posted by: Ly | post date: 02-05-2013 | Subject: PHP




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.