PHP foreach loopThe foreach loop is used to traverse the array elements. It works only on array and object. It will issue an error if you try to use it with the variables of the different datatype. The foreach loop works based on an element rather than an index. It provides the easiest way to iterate the elements of an array. In the foreach loop, we don't need to increment the value. Syntaxforeach ($array as $value) { There is one more syntax of the foreach loop. Syntaxforeach ($array as $key => $element) { Example 1:PHP program to print array elements using a foreach loop. <?php Output: Summer Winter Autumn Rainy Example 2:PHP program to print associative array elements using a foreach loop. <?php Output: Name : onlinequizstock Email : onlinequizstock@gmail.com Age : 22 Gender : Male Example 3:Multi-dimensional array <?php Output: Alex Bob Camila Denial Example 4:Dynamic array
Output: onlinequiz |
0 Comments: