Solved

problem with foreach

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

749 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