Mememe Posted October 24, 2007 Share Posted October 24, 2007 I can't seem to get a grasp of foreach. Can someone explain it more, and show in an example please?Thanks. Link to comment Share on other sites More sharing options...
Synook Posted October 24, 2007 Share Posted October 24, 2007 Foreach loops through each element in an array, and each iteration stores the current element to a variable. So, say we had an array of trangle numbers up to 6, the following code would loop through the array and ech time echo the element the foreach loop is examining $triNums = array(1, 3, 6, 10, 15, 21);foreach ($triNums as triNum) {echo "$triNum, ";} This will echo "1, 3, 6, 10, 15, 21". Link to comment Share on other sites More sharing options...
Mememe Posted October 24, 2007 Author Share Posted October 24, 2007 Foreach loops through each element in an array, and each iteration stores the current element to a variable. So, say we had an array of trangle numbers up to 6, the following code would loop through the array and ech time echo the element the foreach loop is examining$triNums = array(1, 3, 6, 10, 15, 21);foreach ($triNums as triNum) {echo "$triNum, ";} This will echo "1, 3, 6, 10, 15, 21". I kind of get the idea, though I'm confused on the "as triNum" part. Link to comment Share on other sites More sharing options...
Synook Posted October 24, 2007 Share Posted October 24, 2007 That means that each element is stored to the $triNum variable as it comes up - I made a typo, it should be as $triNum not as triNum ! Link to comment Share on other sites More sharing options...
justsomeguy Posted October 24, 2007 Share Posted October 24, 2007 You can also use foreach to get both the index and the value of each element. Run this code and see what happens: <?php$test_array = array( 'name' => 'Steve', 'city' => 'Phoenix', 'age' => 28);foreach ($test_array as $key => $value){ echo "key: {$key}; value: {$value}<br>";}?> More information here:http://www.php.net/manual/en/control-structures.foreach.php Link to comment Share on other sites More sharing options...
Mememe Posted October 25, 2007 Author Share Posted October 25, 2007 You can also use foreach to get both the index and the value of each element. Run this code and see what happens:<?php$test_array = array( 'name' => 'Steve', 'city' => 'Phoenix', 'age' => 28);foreach ($test_array as $key => $value){ echo "key: {$key}; value: {$value}<br>";}?> More information here:http://www.php.net/manual/en/control-structures.foreach.php I kind of understand now. Does $key have to be key, or I can use any name for it with the $ first right? Link to comment Share on other sites More sharing options...
Synook Posted October 25, 2007 Share Posted October 25, 2007 I can use any name for it with the $ first right?Right Link to comment Share on other sites More sharing options...
boen_robot Posted October 25, 2007 Share Posted October 25, 2007 I can use any name for it with the $ first right?And the same goes for $value. Link to comment Share on other sites More sharing options...
Mememe Posted October 25, 2007 Author Share Posted October 25, 2007 Okay, I understand now. Thanks. -Mememe Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.