Imagine that you have an associative array that
you want to iterate through. PHP provides an easy way to use every
element of an array with the Foreach statement.
While a For Loop and While Loop will
continue until some condition fails, the For Each
loop will continue until it has gone through every item in the array.
have an associative array that stores the names of people as the keys with the values being their age.
We want to know how old everyone is at work.
loop to print out everyone's name and age.
The syntax of the foreach
statement is a little strange, so let's talk about it some.
above statement roughly
translates into: For each element of the $employeeAges associative
array I want to refer to the key as $key and the value
The operator "=>" represents the
relationship between a key and value.
You can imagine that the key points => to the value.
example we named the key $key and the value $value.
However, it might be easier to think of it as $name and $age.
example does this and notice how the output is identical because we only changed
the variable names that refer to the keys and values.
Note For more detail please see Php Manual at http://www.php.net
သင့္လက္ထဲ တြင္ associative array တခု ရွိေနၿပီး loop လုပ္၍ တန္ဖိုး တို႔ကို ဆြဲထုတ္ၿပီး အသံုးျပဳ လိုပါက၊ Foreach statement ကိုအသံုး ျပဳရန္ အသင့္ ေတာ္ဆံုး ျဖစ္ပါမည္။
ယခင္ ေဖၚျပ ၿပီးခဲ့ေသာ For Loop and While Loop တို႔တြင္ conditional statement=false ျဖစ္ေန ေသးပါက၊ ဆက္၍ loop လုပ္ေနမွာ ျဖစ္ပါတယ္။ For Each
loop မွာေတာ့ associative array တြင္ရွိ key=>value မ်ားမကုန္ မခ်င္း loop လုပ္ေနမွာ ျဖစ္ပါတယ္။
ေအာက္တြင္ associative array တခု ၏ key=>value မ်ားအား foreach statement ကိုအသံုး ျပဳ၍ တန္ဖိုး တို႔ကို echo လုပ္သြား ပံုကို ေလ့လာပါ။