
Kristian Woods
23,414 PointsHow does the each function in PHP work?
From what I understand, the each() function loops through an array and stores the key/value pairs.
in the example used in this example, she then stores the values in the list() function
<?php
while(list($key, $val) = each($array)) {
"$key => $val\n";
}
?>
so does the list() function take the key/value from the each($array) then store them in the parameters held inside list($key, $val). From there, you can use the parameters in the code block?
Side question: Is the reason you would include the list() function is so that you can use the parameters? is that the only benefit?
Thanks
1 Answer

yalın küçük
4,810 PointsI am new here. Because you included me, I will try to share my opinion but if I confuse you I am sorry.. :)
List takes the elements of $paper array generated by the 'each' and assign its keys and values to the variables then you can use them to print on the screen ( using echo ) or do other things you need. In this example, 'each' returns one key-value pair but there is not any assignment without list as far as I get. And with both list and each, there is not any loop so basically they are inside a loop 'while' ( wrapped inside ). 'While' gives them loop feature ( or property ) until 'each' gives the false return ( when there is not any key and value left in an array.)
But as you know "foreach" for listing purpose is better than "for" in terms of ease ( Based on Alena Holligan's examples). You remember I assume the listing expressions of for ( you create a constant number 0, assign it to zero then increase the number by one for each loop. 'Foreach' was simpler than 'for' in that example. However, I also would like to know the benefit of using 'each' but it is not a loop.
Remember you can echo the same result with different expressions. She is trying to introduce 'each' in a content that she thinks is familiar to us in my opinion. Otherwise I am also learning and did not use each in any project. Actually someone who has deeper understanding of 'each' or someone who actually used it in a real world project may tell us the differences in an example.
<?php
$number = array(1,2,3,4);
$lenght = count($number);
//for syntax you remember below I guess. You create a number. It just represents the key of an array. Because key of the arrays starts with zero, you choose 0 for just simulation purpose. That's all.
for ($x =0; $x < $lenght; $x++){ echo $number[$x]; echo "<br/>"; }
//with foreach syntax and the same result......
foreach ($number as $value){ echo $value . "<br/>"; }
//The same thing
$newarray[0] = "1"; $newarray[1] = "2"; $newarray[2] = "3"; $newarray[3] = "4";
foreach ($newarray as $yeni){ echo $yeni . "<br/>"; }
// Associative Array:.........
$number2 = array( 'Key-0' => "1", 'Key-1' => "2", 'Key-2' => "3", 'Key-3' => "4" );
foreach($number2 as $newkey => $newvalue){ echo $newkey . ":" . $newvalue . "<br/>"; }
reset($number2);
//Now:
//list and each function is kind of an alternative to foreach function. Just list function does not have any function here. It needs returned array key and value pairs.
//In this case for instance:
while(list($key, $value) = each($number2)){ echo $key . ":" . $value . "<br/>"; }
Note: I could not put the code inside a div that has a black background. How do you do that?