Solved

problem with foreach

Posted on 2008-10-04
4
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Compiling PHP with Curl plus protocols 8 47
How do I speed up this PDO query 4 39
how to use jquery variable in mysql query 20 74
Can't find an open element in HTML 5 53
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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

734 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