Solved

problem with foreach

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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