Tuesday, February 8, 2011

Array-For each loops

Assalamu'alaikum and selamat sejahtera

So,what is for each loops?okey for this post we will discuss it and practice it on our code.Here is the code:


$squad_name = array( );
$squad_name["alpha"] = "one";
$
squad_name["beta"] = "two";
$
squad_name["charlie"] = "three";
$
squad_name["delta"] = "four";

foreach ($squad_name as $key_name => $key_value) 
{
print "Key = " . $key_name . " Value = " . $key_value . "<BR>";
}


So,we just concentrate on this line first:


foreach ($squad_name as $key_name => $key_value) 

 so,REMEMBER : foreach NOT for each. Okey,so you put the name of the array,in our case,the name of array is $squad_name.Next is "as $key_name =>$key_value". the key name for this array is alpha,beta,charlie and delta.So,the value of key_name,alpha is one.Do you get the idea?.Besides,you also came named it any name that like this:

foreach ($squad_name as $team_code => $team_number

N/NOTE:next post,we will discuss more on application of this loops

No comments:

Post a Comment