For Each (php)
Mon, 08 Oct 2007 06:23:34
You have an array and you'd like to perform a function on each element. If you're new you may write your script to perform the function on array(0) to array (x) knowing you'll never exceed (x). This will causes "Undefined offset" error messages in your log. What you should be doing is using the foreach function.
Lets take a look at the example from http://uk2.php.net/foreach
0 <?php
1 $YOURARRAY = array(1, 2, 3, 4);
2 foreach ($YOURARRAY as &$value) {
3 $value = $value * 2;
4 }
5 // $YOURARRAY is now array(2, 4, 6, 8)
6 unset($value); // break the reference with the last 7 element
7 ?>
You can change anything in capitals, lowercase dictates required text.
0 = Open tag
1 = define array
2 = for each element in your array do the following
3 = the function for each element
4 = close }
5 = what your new values are
6 = required
7 = close tag
Simple enough, cheers
Ryan Partington
Lets take a look at the example from http://uk2.php.net/foreach
0 <?php
1 $YOURARRAY = array(1, 2, 3, 4);
2 foreach ($YOURARRAY as &$value) {
3 $value = $value * 2;
4 }
5 // $YOURARRAY is now array(2, 4, 6, 8)
6 unset($value); // break the reference with the last 7 element
7 ?>
You can change anything in capitals, lowercase dictates required text.
0 = Open tag
1 = define array
2 = for each element in your array do the following
3 = the function for each element
4 = close }
5 = what your new values are
6 = required
7 = close tag
Simple enough, cheers
Ryan Partington


