Jump to content

Delete every session apart from two


stuckwithcode

Recommended Posts

Hi, I'll explain a little but your best bet is to checkout the manual for how foreach loops are constructed.

 

As you should know, every array has a key and a value. See this array:

 

$something['key1'] = 'val1';

 

is the same as..

 

$something = array('key1' => 'val1');

 

Common foreach loop is:

 

foreach($something as $val){

}

 

 

That feeds the loop the $val for each element of the array. But then we come across the common need for using the $key of an array. So now our foreach uses the pointer in the same fashoin it did in the array() example above.

 

foreach($something as $key => $val){

}

 

 

The confusion here is that the $val is now no longer straight after the 'as' keyword. It is preceded by $key =>

 

I'm not entirely sure of the correct term for this => but it is somekind of array pointer. There is another pointer -> which is used for objects.

foreach ($array as $key => $val) is the same syntax as when you define an array with keys:

 

$array = array(

    'key' => 'val'

);

 

"=>" is just an (array) assignment operator specific to PHP. As far as I know the only other popular language that uses it is PL/SQL from the Oracle platform, for naming parameters in procedure/function calls.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.