Foreach As Loop
Contents |
Foreach As
Description
Loop based on an expression.
foreach( <expression> as <value> ) { statements ... }
Parameters
expression
An expression to use could equal an array, string, whatever.
value
The value to place the result in most common as a $variable.
Remarks
For...As statements may be nested.
If the expression contains an array the loop will cycle through each element in the array.
If the expression contains a string or numbers it will cycle through each char.
It is a bad idea to try edit the array while you are loop through it... Strange things may happen including hard crashes.
If you wish to edit an array as you loop through it check out Each( <array> ) command see its examples one of them should tell you how to do this.
Example
Here we cycle through binary data :
$binary = Pack("ifz0", (int)100, (float)777.42, "Hello"); foreach( $binary as $item ) { println( "Index '" . DecPad($item, 3) . "' " . "Dec '$item' " . "Hex '" . Hex($item) . "' " . "Char '" . Chr($item) . "'" ); }
Here we cycle through an array :
$arr = array( 2, 4, 6, 8, 10 ); foreach( $arr as $item ) { println("Value is $item"); }
Here we cycle through each char in a string :
$arr = "Hello World"; foreach( $arr as $item ) { println("Value is $item"); }
Here we cycle through each letter in a float :
$arr = 777.42; foreach( $arr as $item ) { println("Value is $item"); }
You can also referance when using arrays using the pointer & operator for example :
$arr = array( 2, 4, 6, 8, 10 ); foreach( $arr as & $item ) { println("Value is $item"); // Prints 2 4 6 8 10 $item *= 2; } println(""); foreach( $arr as $item ) { println("Value is $item"); // Prints 4 8 12 16 20 }
See the & before $item on the first loop? This causes $item to actually LINK to the real element inside the array so changing $item will change the item in the array.
This can be seen by printing the array after changing it in the example.
Note - You can only change elements in an array! This will not work with anything else.
A reverse foreach
println("Value $i") foreach("Hello world" as $i);
Example of using Yield here
my $array = array("The", "cat", "and", "the", "dog"); $one = Cat($array); say "One: " . $one; $one = Cat($array); say "One: " . $one; $one = Cat($array); say "One: " . $one; $one = Cat($array); say "One: " . $one; $one = Cat($array); say "One: " . $one; Function Cat( &$array ) { foreach( $array as $i) Yield return $i; } // Prints // One: The // One: cat // One: and // One: the // One: dog // Yield is used to save the position in the array etc // then continue from that position the next time you // do a loop etc // So here it saves the position each time and gives // the next item in the array each time you call the // function
Example of unpacking nested arrays with list()
$array = array( array(1, 2), array(3, 4) ); foreach ($array as list($a, $b)) { // $a contains the first element of the nested array, // and $b contains the second element. echo "A: $a; B: $b\n"; } // Prints // A: 1; B: 2 // A: 3; B: 4
Another example of unpacking nested arrays with list()
$array = array( array(1, 2, 3, 4), array(5, 6, 7, 8) ); foreach ($array as list($a, $b, $c, $d)) { // $a contains the first element of the nested array, // and $b contains the second element. echo "A: $a; B: $b; C: $c; D: $d\n"; } // Prints // A: 1; B: 2; C: 3; D: 4 // A: 5; B: 6; C: 7; D: 8
Yet another example of unpacking nested arrays with list() however this one only works if there is only 1 element in each array (key, value) and it must be a hash and not numeric (like normal arrays)
$array = array( array("Cat" => "Meow"), array("Dog" => "Woof"), array("Foo" => "Bar") ); foreach ($array as list($key, *$value)) { // $a contains the first element of the nested array, // and $b contains the second element. echo "Key: $key | Value: $value\n"; } // Prints // Key: Cat | Value: Meow // Key: Dog | Value: Woof // Key: Foo | Value: Bar