Foreach As Key Value Loop
From Sputnik Wiki
(Difference between revisions)
(Created page with "= Foreach As Key Value = === Description === Loop based on an expression. <pre> foreach( <expression> as <key> => <value> ) { statements ... } </pre> === Parameters =...") |
|||
Line 61: | Line 61: | ||
println( "Key '$key' | Value '$value'" ) Foreach ($var as $key => $value); | println( "Key '$key' | Value '$value'" ) Foreach ($var as $key => $value); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Revision as of 14:32, 8 August 2014
Contents |
Foreach As Key Value
Description
Loop based on an expression.
foreach( <expression> as <key> => <value> ) { statements ... }
Parameters
expression
An expression needs to be an array for this loop to happen.
key
The variable to place the key in most common as a $variable.
value
The variable to place the value in most common as a $variable.
Remarks
For...As..Key..Value statements may be nested.
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 :
$var = array( "One" => 100, "Two" => 200, "Three" => 300 ); Foreach ($var as $key => $value) { println( "Key '$key' | Value '$value'" ); }
A reverse foreach
$var = array( "One" => 100, "Two" => 200, "Three" => 300 ); println( "Key '$key' | Value '$value'" ) Foreach ($var as $key => $value);