Jump to content

foreach (array as value)


watagal

Recommended Posts

Is there a way to loop through an array backwards using "foreach"?

foreach (array as value){	code to be executed;}

BTW, this is a multi-dimension array, so if array_reverse() is an option, whats the syntax for reversing a 2-dimensional array?Thanks, Gal

Link to comment
Share on other sites

how does the array look like and do u only want the things in the array to be backwards, or also the things in the arrays that are in the arrays?array( array('value 1','value 2') array('value 3','value 4') array('value 5','value 6'))after array_reverse it would be like:array( array('value 5','value 6') array('value 3','value 4') array('value 1','value 2'))if you want the arrays in it also to be backwards, you can just first use array_reverse before the foreach(), and then in the foreach($array as $value){} use array_reverse for each $valueif you dont wanna use array_reverse, you can also use for($i=0,$count=count($array);$i<$count;$i++){}and instead of using $array[$i], use $array[$count-$i]

Link to comment
Share on other sites

Thanks Wander-I'm really weak on arrays in PHP, so here's what I'm trying to do. I'm building a multi-dimensional array to build a tree type menu based on only a node ID and its corresponding parent's node ID (both query in the database, sorted on the PID).

$pid	= -1;$grpknt	= -1;while ($record = mysql_fetch_array($results)) {	$id = record['id'];		$photoTag = Array(		'"'.$id.'"' => Array(			"name" => $record['name'],			"desc" => $record['description'],			"apid" => $record['alt-pid'],			"mid"  => $record['memberId']		)	);		if ($pid != $record['pid']) {		$pid  = $record['pid'];		$grpknt++;		$eleknt = 0;		$group[$grpknt][0] = $pid;				// element[0]  = PID (parent ID)	}	$eleknt++;	$group[$grpknt][$eleknt] = $id;					// element[1+] = IDs (children ID)		$node[$id] = '@';						// Default = item, not folder}$group_rev	= array_reverse($group);foreach (array_reverse($photoTag) as $id => $r) {	// rec[knt][attribute] -> r[attribute]	foreach ($group_rev as $grp) {					// group[grpknt][eleknt] -> g[eleknt]		$pid = array_shift($grp);					// first element is always PID, remainder are children IDs		if ($id == $pid) {							// ID found in Group array			$node[$id] = Array;			$k=-1;			foreach ($grp as $kid) {				// loop thru group array				$k++;				if ($node[$kid] != '@') {					$node[$id][$k] = Array($node[$kid]);				} else {					$node[$id][$k] = $kid;				}			}		}	}}

I think I have the logic (untested), but I'm struggling with the array syntax.Am I anywhere close to constructing the $node[] array?-Gal

Link to comment
Share on other sites

if you dont wanna use array_reverse, you can also use for($i=0,$count=count($array);$i<$count;$i++){}and instead of using $array[$i], use $array[$count-$i]
You can also use for ($i = count($array) - 1; $i >= 0; $i--) and use $array[$i].
Am I anywhere close to constructing the $node[] array?
Throw in some print_r statements to see what the array looks like, it makes it easier to work with if you can see an example of the structure. After you build $group do print_r($group) to see what it looks like. It helps to view the source of the page instead of looking at it in the browser. You're not using the $photoTag array correctly though. You build it in the first while loop, but never assign it to anything. So each time through the while loop you overwrite it. After the while loop ends then $photoTag will be whatever it was set to the last time through the loop. After the while loop ends you can use print_r($photoTag) to see that. You'll also notice that the index with the ID value includes quotes. But $photoTag only has 1 element since you keep overwriting it. If you want to add to it you can either use array_push or the shortcut syntax:$photoTag[] = array(...);
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...