?
Solved

problem with foreach

Posted on 2008-10-04
4
Medium Priority
?
299 Views
Last Modified: 2008-10-04
I want to loop over an array in a foreach. Inside the forreach I am creating som plots for a JPGraph.

It loops fine, but the problem is that it overwrites the data for each loop. How do I avoid this? Variable variable names?
foreach ($sensorArray as $sensor) {	
	$data = $db->getDataSet($sensor, $hoursback);
	$alias = $db->getSensorName($sensor);
	$xdata = array_keys($data);
	$ydata = array_values($data);
	$line2 = new LinePlot($ydata, $xdata);
	$line2->SetLegend($alias);
	$line2->SetColor('#0000FF');
	$line2->mark->SetWidth(4);
	$graph->Add($line2);
}

Open in new window

0
Comment
Question by:dreamer81
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Ali Kayahan
ID: 22640212
Why dont you use arrays ? ;
foreach ($sensorArray as $sensor) {      
      $data[] = $db->getDataSet($sensor, $hoursback);
      $alias[] = $db->getSensorName($sensor);
      $xdata[] = array_keys($data);
      $ydata[] = array_values($data);
      $line2[] = new LinePlot($ydata, $xdata);
      $line2->SetLegend($alias);
      $line2->SetColor('#0000FF');
      $line2->mark->SetWidth(4);
      $graph->Add($line2);
}
   If this is not what you are looking for , than can you please explain in a detailed way ?
0
 
LVL 1

Author Comment

by:dreamer81
ID: 22640544
when I called $graph->Add($line2); the first time in the loop it adds the first graph

when it is called the next time in the loop $line2 reference is overwritten, you know?
0
 
LVL 14

Accepted Solution

by:
Ali Kayahan earned 750 total points
ID: 22640633
  I am not so clear with the issue , but clearstatcache() or unset($line2) commands inside loop may solve your issue,
 
   it seems that youare trying to get new values each time run the for each from other objects , but foreach will only iterate the related array (sensorArray this time and $sensor 's value will change) ,
0
 
LVL 1

Author Comment

by:dreamer81
ID: 22641159
unset($line2) did the job...

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question