Solved

problem with foreach

Posted on 2008-10-04
4
255 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now