How to find the index of foreach loop in PHP ?
Last Updated :
27 Sep, 2019
The foreach construct provides the easiest way to iterate the array elements. It works on array and objects both. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively. It allocates temporary memory for index iterations which makes the overall system to redundant its performance in terms of memory allocation. An error will be issued, if we will try to use foreach loop with uninitialized variable or a variable with different data types.
Syntax:
foreach( $array as $element ) {
// PHP Code to be executed
}
or
foreach( $array as $key =>> $element) {
// PHP Code to be executed
}
Return Values:
- $key: This variable holds the key index of current element/
- $value: In each iteration, the current value of array element is assigned to the $value variable.
Program 1:
php
<?php
// Declare an array
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Iterate through the array using foreach
// construct and store the key and its value
// Use foreach loop to display the
// key of allelements
foreach ($arr as $key => $value) {
echo "key = " . $key . ", value = "
. $value . "\n";
}
?>
Output:
key = 0, value = 1
key = 1, value = 2
key = 2, value = 3
key = 3, value = 4
key = 4, value = 5
key = 5, value = 6
key = 6, value = 7
key = 7, value = 8
key = 8, value = 9
key = 9, value = 10
Program 2:
php
<?php
// Declare an array
$arr = array(
"a" => "Welcome",
"b" => "to",
"d" => "GeeksforGeeks"
);
// Iterate through the array using foreach
// construct and store the key and its value
// Use foreach loop to display the
// key of allelements
foreach ($arr as $key => $value) {
echo "key = " . $key . ", value = "
. $value . "\n";
}
?>
Output:
key = a, value = 1
key = b, value = 2
key = c, value = 3
key = d, value = 4
Program 3:
php
<?php
// Declare a multi-dimensional array
$arr = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
// Iterate through the array using foreach
// construct and store the key and its value
// Use foreach loop to display the
// key of allelements
foreach ($arr as $keyOut => $out) {
foreach($out as $keyIn => $value) {
echo "key = (" . $keyOut . ", " . $keyIn
. "), value = " . $value . "\n";
}
}
?>
Output:
key = (0, 0), value = 1
key = (0, 1), value = 2
key = (0, 2), value = 3
key = (1, 0), value = 4
key = (1, 1), value = 5
key = (1, 2), value = 6
key = (2, 0), value = 7
key = (2, 1), value = 8
key = (2, 2), value = 9
Similar Reads
How to re-index an array in PHP? The re-index of an array can be done by using some inbuilt function together. These functions are: array_combine() Function: The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values.
5 min read
How to re-index an array in PHP? The re-index of an array can be done by using some inbuilt function together. These functions are: array_combine() Function: The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values.
5 min read
How to break an outer loop with PHP ? In PHP, breaking an outer loop refers to exiting a parent loop from within a nested loop. This can be achieved using the break statement with a numeric argument indicating how many levels of loops to exit. It helps control complex loop flow efficiently.Table of ContentUsing break keywordUsing goto k
4 min read
How to Use Foreach Loop with Multidimensional Arrays in PHP? Given a Multi-dimensional array, the task is to loop through array in PHP. The foreach loop is a convenient way to iterate over arrays in PHP, including multidimensional arrays. When dealing with multidimensional arrays, you can use nested foreach loops to access and process each element. In this ar
2 min read
How to Iterate Over an Array in PHP? Arrays are fundamental data structures in PHP, and the ability to iterate through them is crucial for manipulating and processing data. In this article, we will explore various approaches to iterate through arrays in PHP.Table of ContentUsing for LoopUsing foreach LoopUsing while Loop with each() Fu
3 min read
How to loop through an associative array and get the key in PHP? Associative arrays are used to store key-value pairs. For example, to store the marks of the different subject of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subjectâs names as the keys in our associative array, and the value
3 min read