PHP- foreach Loop



PHP


In this article, we will go over the PHP foreach loop.

The foreach loop allows us to print out all the contents of an array.

The foreach loop goes through each element of an array, one by one, and using an echo statement, we can print out the contents of an array. The number of times the loop will execute will be equal to the number of elements in the array.

There are 2 ways for that the foreach loop can be used. If an array only has values assigned to them, with no keys, we use the foreach loop, only to specify the values of each of the elements in the array. This is shown in the first example. The 2nd way we can use the foreach loop is with arrays with the keys and values specified. This is shown in the second example.

foreach Loop with Only Values

Say, that we have an array that only has values specified, and not keys.

For example, let's take the following array below:

$ages= array(10, 12, 15, 16, 17);

For an array like this, many times we are only interested in the values of the elements of the array. Even though PHP creates the keys 0, 1, 2, 3, 4 as an index, many times we aren't interested in these values. We just want to know the values of the elements in the array.

Using the foreach loop in the following format below, we can print out all the values of the above array:

<?php>

$ages= array(10, 12, 15, 16, 17);
foreach ($ages as $value)
{
echo "$value<br>";
}

?>


Actual PHP Output

10
12
15
16
17


You can see that the foreach loop prints out all of the elements of the array.

Realize that although the variable $value is used with the as keyword, it can be named anything. We can change value to $value to $kids, so that our new code is:

<?php>

$ages= array(10, 12, 15, 16, 17);
foreach ($ages as $kids)
{
echo "$kids<br>";
}

?>


In other words, PHP will know that the variable after the as keyword is the variable which stores the values of the array, and it can be named anything.


Foreach loop with Keys and Values

The other way a foreach loop can be used is to specify both the keys and values of an array.

Let's say we have the following array below:

$children= array("John"=>11, "Peter"=>12, "Bill"=>10);

Here in this array, the keys are John, Peter, and Bill. The values are 11, 12, and 10. Here, the keys represent the names of the children and the values represent their ages. So when printing out the array, we want to know and print out both the keys and values of each element of the array.

And we can do this, using the foreach loop in the following format:

<?php

foreach($children as $key=>$value)
{
echo "$key is $value years old<br>";
}

?>


Actual PHP Output

John is 11 years old
Peter is 12 years old
Bill is 10 years old


Here print out all the children names along with their ages.

Again, just like with the first loop, we don't have the use the variables $key and $value; we can really names these variables anything. For example, we can change them to $name and $age, so that you can know what each one represents.

So we can change the above code to:

<?php

$children= array("John"=>11, "Peter"=>12, "Bill"=>10);
foreach($children as $name=>$age)
{
echo "$name is $age years old<br>";
}

?>

And it will work the same as the before code.

Printing the key and value of an array can also work for arrays where only values are specified.

Say, we have the following array:

$children_ages= array(10, 9, 8, 12, 13, 7);

The keys which will be created for this array by PHP are 0-5, to represent each element.

Even though we created an array and only specified values, we can also print out the assigned keys which PHP creates for them, starting with index 0 and going up 1 each successive element.

To print out the above array $children_ages, we use the code:

<?php

$children_ages= array(10, 9, 8, 12, 13, 7);
foreach ($children_ages as $key=>$value){
echo "Key $key is $value$lt;br>";
}

?>


Actual PHP Output

Key 0 is 10
Key 1 is 9
Key 2 is 8
Key 3 is 12
Key 4 is 13
Key 5 is 7


So even though we did not specify keys with the array, PHP automatically assigns integer keys to the array, and we can output each integer keys with the code above.

And this is how foreach loops work. They go through each element of an array and can echo out either the value alone or the key and the value.


HTML Comment Box is loading comments...