Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for loops

Status
Not open for further replies.

wolf73

Programmer
Feb 12, 2006
93
CA
I have never seen these kind a for loops,

foreach ($buyers as $buyer=>$weight)
what are we doing with in ($buyers as $buyer=>$weight),
especially $buyer=>$weight?
 
You're not looking at a for loop. You're looking at a foreach loop. The two are similar, but different, things.

A foreach loop loops through the elements of an array. By using the construct:

foreach ($a as $id => $value)

the foreach loop will loop through all the elements of $a, and for each element, $id will take on the index of that element and $value will take on the value of that element. This construct is very useful for looping through associative arrays.

See for the PHP online manual's discussion of this construct.

Also, play with this code:

Code:
<?php

$a = array ('a' => 'Adam', 'b' => 'Betty', 'c' => 'Charles', 'd' => 'Darlene');

print '<html><body><pre>';
foreach ($a as $index => $value)
{
	print 'Array index: ' . $index . ', value: ' . $value;
	print "\r\n";
}
print '</pre></body></html>';
?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
But beware. Foreach works with copies. So in the above example $value is a copy of the value in the original array. Modifying this value will not alter the original value in the array.

mcvdmvs
&quot;It never hurts to help&quot; -- Eek the Cat
 
although in php > 5 this works fine :
Code:
$array = array (3,5,7);
foreach ($array as &$value):
//modify array element
$value++;
endforeach;
 
If you're going to be reading php code which is not your own, get very familar with 3 things that you may not see very much of in other languages (you may, especially if other is perl or python, but you may not if other is C++ or some such)

foreach
each
? :

(the last one is available in most languages, but I see it used more in PHP than others.)
 
I am new to PHP and working with a previous programmer's code. I am working with the following foreach loop (please note $x):

Code:
<?
	foreach($tasks as $item) {
$x=$x++;
		
		$title = stripslashes(ucfirst($item['subject']));

		// If this task was completed
		if (isset($item['date_complete'])) {
			$title = 'Completed ('.date('g:ma', $item["date_complete"]).'): '.$title;
			$css_class = 'task_complete';
		}
		else {
			$css_class = 'task_incomplete';
		}
?>
	<div class="item" onclick="view_task(<? echo $item['id']; ?>)" title="<? echo $title; ?>">
	<a href="javascript:view_task(<? echo $item['id']; ?>);">
			<span class="<? echo $css_class; ?>">&nbsp;&nbsp;<? echo $item['prior']; ?>&nbsp;&nbsp;&nbsp;&nbsp;<? echo omit_after($item['subject'],20); ?></span>
		</a>
	</div>
<?
	
	}
?>
<?
	
	if ($x <4){
	for($x; $x<4; $x++){
?>
	<div class="item" title="Add a task...">
		<div>
			<a href="javascript:add_task(<? echo date("Y,m,d",$this_date['time']); ?>)">
				<span class="add_item">add&hellip;</span>
			</a>
		</div>
	</div>
<?
	}
	}
?>

$x however, is always 0. I am trying to count each item through the foreach loop. I'm obviously doing it wrong as my "add" row prints 5 rows every time. Any suggestions would be appreciated? Apologize for being so green. Thanks...

-trix
"Wiggle your big toe.
 
four things:

1. change
Code:
$x=$x++;
to
Code:
$x++;
2. instantiate the variable before the loop
Code:
$x=0;
3. i dont think this will work
Code:
for($x; $x<4; $x++)
. i'd rather see
Code:
for($i=$x; $i<4; $i++)
4. don't hijack threads! This is a new issue - create a new thread. that way it is easier for people coming later to see your issue, the solution to it and learn by the interaction.
 
Sorry about that didn't mean to hijack. Thanks very much for your help!!

-trix
"Wiggle your big toe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top