![]() Now let’s look at a case where we pass a second argument.Īs you can see the key and the values of the associative array were printed. The output of the above code snippet would be: name: Eric PHP Foreach() on an Associative array: "Eric", The output of the above code snippet would be: Hire In this section, we first look at how the foreach() function works on an indexed array followed by which we look at it’s working on an associative array. “$value” is a variable that stores the current element in each iteration.Īssociated array, uses keys and values, and hence the $key & $values in the second syntax represent the same accordingly. It is the array or the variable containing the array. ![]() Here, “Iterable” is the required parameter. The syntax for associative arrays: foreach (iterable as $key => $value) The syntax for indexed arrays is as given in the following code block: foreach (iterable as $value) The foreach() method has two syntaxes, one for each type of array. This allows you to run blocks of code for each element. It can also be used to iterate over objects. They go through each element of an array and can echo out either the value alone or the key and the value.The foreach() method is used to loop through the elements in an indexed or associative array. 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.Īnd this is how foreach loops work. To print out the above array $children_ages, we use the code: The keys which will be created for this array by PHP are 0-5, to represent each element.Įven 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 ![]() Printing the key and value of an array can also work for arrays where only values are specified. Here print out all the children names along with their ages.Īgain, just like with the first loop, we don't have the use the variables $key and $value we can really names these variables anything.įor example, we can change them to $name and $age, so that you can know what each one represents.Īnd it will work the same as the before code. So when printing out the array, we want to know and print out both the keys and values of each element of the array.Īnd we can do this, using the foreach loop in the following format: Here, the keys represent the names of the children and the values represent their ages. Here in this array, the keys are John, Peter, and Bill. Let's say we have the following array below: The other way a foreach loop can be used is to specify both the keys and values of an array. 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. We can change value to $value to $kids, so that our new code is: Realize that although the variable $value is used with the as keyword, it can be named anything. You can see that the foreach loop prints out all of the elements of the array. Using the foreach loop in the following format below, we can print out all the values of the above array: Though PHP creates the keys 0, 1, 2, 3, 4 as an index, many times we aren't interested in these values. Say, that we have an array that only has values specified, and not keys.įor example, let's take the following array below:įor an array like this, many times we are only interested in the values of the elements of the array.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |