Author Topic: Foreach Value's into a variable??? Need help.  (Read 112 times)

0 Members and 1 Guest are viewing this topic.

Offline hinchcliffeTopic starter

  • Irregular
    • View Profile
Foreach Value's into a variable??? Need help.
« on: February 03, 2010, 07:56:06 PM »
Hello, Is there a way I can echo an for each variable outside the for each statement?


<?php
	
foreach (
$products as &$value) {
	
	
$partnumber '<PartNumber>'.$value["sku"].'</PartNumber>';
	
	
echo 
$partnumber;
	
	

	
}
?>


I need $partnumber to  be echoed in another piece of php code with out using the for each to grab each value.

How can this be done?

*EDIT* - Crap I posted in the wrong forum, still if you can help this would be nice.
« Last Edit: February 03, 2010, 07:57:37 PM by hinchcliffe »

Offline khr2003

  • Enthusiast
  • Gender: Male
    • View Profile
Re: Foreach Value's into a variable??? Need help.
« Reply #1 on: February 03, 2010, 08:20:13 PM »
hi
you can add an array that grabs the values of the foreach loop and then read it somewhere else, like this:
<?php
foreach ($products as &$value) {

$partnumber '<PartNumber>'.$value["sku"].'</PartNumber>';
	


$array[$value] .= $value;

}

// then outside the loop

echo $array[The value of the array to be printed];
?>

Offline hinchcliffeTopic starter

  • Irregular
    • View Profile
Re: Foreach Value's into a variable??? Need help.
« Reply #2 on: February 08, 2010, 01:59:41 PM »
Thank you, this worked like a charm.

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.