0

Having:

$a as $key => $value;

is the same as having:

$a=array();

?

5
  • You made a typo, first statement should be: $a as $key => $value; Commented Sep 7, 2010 at 12:01
  • 4
    I'm not sure I understand what you're asking. $a = array() just initializes $a to an empty array, while foreach() loops over an existing array. Commented Sep 7, 2010 at 12:02
  • $a as $key = $value; isn't even a valid PHP statement. Commented Sep 7, 2010 at 12:04
  • @Fanis et all: Yes. I'm wondering if $a as $key=$value means, for each line, "put $a on a new array upon certain conditions". Commented Sep 7, 2010 at 14:16
  • $a as $key=$value will throw an error, as this syntax is not supported. Commented Sep 7, 2010 at 14:20

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

It's the T_DOUBLE_ARROW Parser Token. You use it to denote a key belonging to a value in an array.
Thanks a lot Gordon. Kind a hard to find the meaning of => using it as a search string on google ;)
@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. 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.
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.
1

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.

1 Comment

That in makes so much difference on this comprehension. Thanks a lot.
1

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').

1 Comment

Thanks. That help me see a difference.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.