Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Arrays and Control Structures PHP Loops While Listing Array Values

How 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

2 Answers

yalın küçük
yalın küçük
4,810 Points

I 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?

Josh Coast
Josh Coast
5,112 Points

looks like the each has been replaced with

foreach ($array as $key => $val) $$key = $val;

because the pattern in this video is slower than a foreach.

https://www.webmasterworld.com/php/5003284.htm

Personally, the foreach makes way more sense in my head anyway. This while(list($key, $val) = each($array)) { pattern is really confusing.