views:

52

answers:

4

Having:

$a as $key => $value;

is the same as having:

$a=array();

?

Thanks in advance, MEM

+1  A: 

First of all it has to say

foreach($a as $key => $value)

Then, as far as I know,

foreach($a = array())

doesn't compile.

That said, if you foreach, you iterate through the elements of an array. With the 'as' keyword, you get pairs of key/value for each element, where $key would be the index by which you can get $value:

$value = $a[$key];

Did this answer your question? If not, please specify.

edit:
In other programming languages it would spell something like

foreach($key => $value in $a)

or (C#)

foreach(KeyValuePair<type1, type2> kv in a)

which I think is more intuitive, but basically the same.

Martin
That in makes so much difference on this comprehension. Thanks a lot.
MEM
+1  A: 

if you have the following:

$a = array('foo' => array('element1', 'element2'), 'bar' => array('sub1', 'sub2'));

if you use $a as $key=> $value in the foreach loop,

$key will be 'foo', and $value will be array('element1', 'element2') in the first iteration, in the second $key == 'bar' and $value == array('sub1', 'sub2').

Femaref
Thanks. That help me see a difference.
MEM
+3  A: 

No, it's not. It's more like

list($key, $value) = each($arr);

See the manual

foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}

is identical to

$arr = array("one", "two", "three");
reset($arr);
while (list($key, $value) = each($arr)) {
    echo "Key: $key; Value: $value<br />\n";
}

Also see

Gordon
What does the => mean?
MEM
It's the [`T_DOUBLE_ARROW` Parser Token](http://php.net/manual/en/tokens.php). You use it to denote a key belonging to a value in an [array](http://www.php.net/manual/en/language.types.array.php#language.types.array.syntax).
Gordon
Thanks a lot Gordon. Kind a hard to find the meaning of => using it as a search string on google ;)
MEM
@MEM if you do not know basic things like this yet, I strongly encourage you to go through the [Language Reference of the PHP Manual](http://de.php.net/manual/en/langref.php). It's a lot to read, but it's well writenn and worth the read to get an idea of how the language works. The manual is actually one of the things where PHP really shines.
Gordon
Thanks a lot. I have used => for some months already... and I understand enough (to solve some issues). But I really miss a solid (from the ground) formation, with a more expert developer. Normally however, I do found experienced developers, but only those that don't use filter_input, PDO, design patterns, subversion systems... and is the path I was looking for. I will take your like as a reference. :) (I do take php.net as one already and I tried to search for "=>" no luck however) :D Cheers.
MEM
A: 

If you loop over an array using foreach, the array is the first expression inside the parenthesis.

It can be a variable, like $a, or an array literal like array(1, 2, 3). The following loops are identical:

With an array literal:

foreach(array(1, 2, 3) as $number)
    print($number);

With a variable:

$a = array(1, 2, 3);
foreach($a as $number)
    print($number);
geon