473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Looping through arrays with foreach

Summary: Use foreach(..) instead of while(list(..)=each(..)).

--=[ How to use foreach ]=--

Foreach is a language construct, meant for looping through arrays.
There are two syntaxes; the second is a minor but useful extension
of the first:

foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement

For example:
<?php
$arr = array("one", "two", "three");
foreach ($arr as $value) {
echo "Value: $value\n";
}
?>

This will output:
Value: one
Value: two
Value: three
--=[ Why not to use list & each ]=--

Another method to loop through an array is the following:
<?php
while (list(, $value) = each($arr)) {
echo "Value: $value<br />\n";
}
?>

This has some disadvantages:
- It is less clear, because list() looks like a function but
actually works the other way around: instead of returning
something, it takes a parameter by being the left-hand side
of the expression and assigns values to the parameters.
- You have to reset() the array if you want to loop through it
again.
- It is approx. three times slower than foreach.
--=[ About for & count ]=--

Another method to loop through an array:
for ($i = 0; $i < count($arr); $i++) {
echo "value: $arr[$i]\n";
}

Consider this array:
array(5 => "Five");

The above for-loop would loop from elements 0 through 5, while
only one element exists. If this is what you want, the for-loop
is an effective way to loop through an array. If not, use
foreach instead.

Mar 31 '06 #1
4 2545
Sjoerd said the following on 31/03/2006 11:29:
Another method to loop through an array:
for ($i = 0; $i < count($arr); $i++) {
echo "value: $arr[$i]\n";
}

Consider this array:
array(5 => "Five");

The above for-loop would loop from elements 0 through 5, while
only one element exists.


Actually, no it wouldn't. count($arr) == 1, so it attempts to access
only $arr[0], which clearly doesn't exist.


--
Oli
Mar 31 '06 #2
Sjoerd wrote:
Another method to loop through an array is the following:
<?php
while (list(, $value) = each($arr)) {
echo "Value: $value<br />\n";
}
?>


The main problem with the list() = each() construct is that it has a
side-effect--namely that of advancing the array's internal pointer.
Forgetting to call reset() on an array was a common mistake in PHP 3.

Mar 31 '06 #3
It should be said that foreach operates on a copy of the array.
Changes to $value won't will not affect the original array.

With PHP5, the referential foreach was added, which aliases $value to
the original value in the array.
foreach($array as $key => & $value) { ... }
It should also be noted that $value will still exist as a reference
after the foreach() is over, thus assignments to $value will affect the
array also, so you much unset() it to prevent undesired side effects.

More information is available in the manual at:
http://us2.php.net/manual/en/control...es.foreach.php

Mar 31 '06 #4
Sjoerd wrote:
Another method to loop through an array is the following:
?php
while (list(, $value) = each($arr)) {
echo "Value: $value<br />\n";
}
?

The main problem with the list() = each() construct is that it has a
side-effect--namely that of advancing the array's internal pointer.
Forgetting to call reset() on an array was a common mistake in PHP 3.

Mar 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bart Nessux | last post by:
How is this done in php? I've tried several variations of the following: contact_array = ($_POST, $_POST, $_POST, $_POST, $_POST); The ultimate goal is to insert the array into a table in...
2
by: Sturnoff Megantic | last post by:
This seems like such a stupid little problem, but it's been bugging me for hours now. I have a 2D array called agents2d. I need to loop through it and display the info. Here is my code: echo...
2
by: paul | last post by:
Hi all, I have a 2D array, which I am trying to access, but I am having problems doing this: If, for instance, I use a nested for loop to go through the values, using something like echo...
2
by: John Dalberg | last post by:
In a traditional for loop, one can use the index (say i) and continue looping in another function and come back to original for loop (even though it's not good practice) Can the newer foreach...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
6
by: stefano.troiani | last post by:
Hi, i have two arrays and I would like to loop both of them one inside the other. The first one comes from a readdir and present an array with the document's names, the second one is an array...
1
by: javedna | last post by:
Can PHP help with the following as I have tried in the MYSQL Forums and cant get any help Thanks Nabz ---------------------------------------- Hi I am developing a PHP MYSQL questionnaire...
1
by: assgar | last post by:
Hello I have changed the process code abit so it receives the data from the form and ensures the data in array format. This has eliminated my previous error. The problem I am experiencing...
110
by: fjm | last post by:
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.