0

Good evening or morning everyone..
Is there any way to understand this:
$array = ('element 1','element 2','element 3');
while($x=each($array)){
echo $x.' Value:'.$x;
}
I mean where did the 'key' and 'value' come from although they're not defined in the file at all??

5
Contributors
8
Replies
10
Views
6 Years
Discussion Span
Last Post by edwinhermann
0

Sorry, I've forgotten to enclose the code with coding tags.

0

I agree with evstevemd.

foreach ($array as $key => $value) {
  echo 'The key is '.$key.' and the value is '.$value; 
 }

$key and $value are created as part of the foreach statement.

0

See the manual page for each. It's in the third section (return values).

Thank you. I've found the solution.

0

I agree with evstevemd.

foreach ($array as $key => $value) {
  echo 'The key is '.$key.' and the value is '.$value; 
 }

$key and $value are created as part of the foreach statement.

Thank you, but that's not my question at all.

0

Thank you, but that's not my question at all.

Well the answer I provided is the same as the answer to your question. Perhaps I assumed you'd make the link. So, to be explicit:

I mean where did the 'key' and 'value' come from although they're not defined in the file at all??

'key' and 'value' are keys of the array $x which are created as part of the each() function.

Edited by edwinhermann: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.