In this article, I briefly shared several ways to loop through the array and output array values in some common programming languages.
1. In php code, we generally choose for and foreach to loop the array:
Example:
1. For loop
$dyshuzu = array('text1','text2','text3');
$jishu = count($dyshuzu);
for($i=0;$i<$jishu;++$i){
echo $dyshuzu[$i]."<br>";
}
2. Foreach loop
$dyshuzu = array('text1','text2','text3');
foreach($dyshuzu as $mgsj){
echo $mgsj."<br>";
}
In the above two examples, we have defined the array $dyshuzu. In the for loop statement, the array to be traversed must be an index array. The count() method must first calculate the number of arrays in the array, and then define the variable i. Execute the loop sequentially $i=$i+1, and use the array name [$i] to indicate that the data in a single array is eco output on the page.
In each loop of foreach, the value of the current element is assigned to $mgsj, which automatically moves the pointer in the array one step back. So in the next loop, you can get the next element of the array until the end of the array is traversed. Compared with the for loop, the effect of using the foreach loop statement will be higher. Of course, it is okay in php.
Use list, each, and while together with a combined loop array, but it is not recommended.
2. Commonly used in js code are for, for-in, and array methods. ForEach, for-of loops:
1. For loop
The for loop in JavaScript will record the index and value of each element of jssj:
const jssj = ['text1','text2','text3'];
for (var i=0; index <jssj.length; i++) {
console.log(jssj[i]);
}
The for loop in js code has a wide range of uses. It is very useful if you don’t want to start looping from the first array element
2.forEach()
const jssj = ['text1','text2','text3'];
jssj.forEach((zhi, suoying) => {
console.log(zhi, suoying);
});
The forEach method is more convenient. It can access the index and elements in the array without performing redundant operations, but it cannot exit early after the loop starts. ForEach() loop, while the break can be used in the for() method loop. Exit the loop.
3. for-in
const jssj = ['text1','text2','text3'];
for (const key in jssj) {
console.log(key);
}
The for-in loop method accesses the key, not the value. The index of the element in the array is a string, not a number. It can traverse all enumerated properties of the object.
4. For-of loop
const jssj = ['text1','text2','text3'];
for (const zhi of jssj) {
console.log(zhi);
}
For-of is a relatively new looping method nowadays. It is more effective when looping an array. It can not only traverse the array, but also loop iterable objects, and use break and continue as external scopes. The price-performance ratio of the for-of loop is better than that of for, for-in, and .forEach.
Three, the loop array method in python real code
1. Use for in to traverse the array like other programs
pyshuzus = ["Array1","Array2","Array3"]
for pyshuzu in pyshuzus:
print pyshuzu
2. First press to get the length of the array, and then you can traverse the array according to the index number of the array object
pyshuzus = ["Array1","Array2","Array3"]
for i in range(0, len(pyshuzus)):
print i, pyshuzus[i]
Four, traversal array method in java
1. For loop
public class Test1 {
public static void main(String[] args){
String[] shuzu = new String[5];
shuzu[0] = "Text 1";
shuzu[1] = "Text 2";
shuzu[2] = "Text 3";
for(String ww:shuzu){
System.out.println(ww);
}
}
}
The above are some ways of looping through arrays in some common programming languages.Basically, they all have a for() method to traverse arrays.