Concept
How can you iterate through an array using foreach?
ComputerScienceOne / Adding Elements
"For each key value pair:\n\nforeach($arr as $key => $val) {\n\nprint \"$key maps to $val\n\n\";\n\n}\n\nThis syntax gives you access to both the key and the value for each element in the array $arr. The keyword as is used to denote the variable names $key and $val that will be changed on each iteration of the loop. You need not use the identifiers $key and $val; you can use any legal variable names for the key/value variables. If you do not need the keys when iterating, you can use the following shorthand syntax.\n\nFor each value:\n\nforeach($arr as $val) {\n\nprint \"$val\n\n\";\n\n}"
Related Ideas
- How can you iterate over indexed and associative arrays in PHP?ComputerScienceOne · Iteration
- How does PHP use foreach loops to iterate over array elements?ComputerScienceOne · Foreach Loops
- How does the foreach syntax iterate over an array in PHP?ComputerScienceOne · Normalizing a Number
- How are foreach loops used to iterate over array elements in PHP?ComputerScienceOne · Do-While Loops
- How can key-value pairs be initialized in a PHP array?ComputerScienceOne · Non-Contiguous Indices
- How can you simulate a foreach loop when iterating over an array in C?ComputerScienceOne · Other Issues
- How can you extract array keys and values and compare arrays in PHP?ComputerScienceOne · Iteration
- How can you iterate over Java arrays?ComputerScienceOne · Basic Usage