Jump to content

Arrays


stownsend

Recommended Posts

Hello

 

I'm having a few issues with arrays and strings and was after some help / pointers.  I need an array laid out as the following for a charting library:-

 

array(array(array('2015-06-22 20:20:11', 19.69),array('2015-06-22 19:47:09', 20.56),array('2015-06-22 19:30:38', 21.06),array('2015-06-22 19:14:06', 21.69),array('2015-06-22 18:08:02', 23.63),array('2015-06-22 17:18:29', 25.25)))

 

I'm using mySql and a loop to generate each array, and when when I echo out the string ($arrayOutSideTempData) I get the following...

 

array('2015-06-22 20:20:11', 19.69),array('2015-06-22 19:47:09', 20.56),array('2015-06-22 19:30:38', 21.06),array('2015-06-22 19:14:06', 21.69),array('2015-06-22 18:08:02', 23.63),array('2015-06-22 17:18:29', 25.25)

 

To get the correct variable i then have used:

 

$p->data = array(array($arrayOutSideTempData));

 

nothing is returned but when I have replaced $arrayOutSideTempData with the actual data of  array('2015-06-22 20:20:11', 19.69),array('201....... its worked ?

 

Am i missing something ????

 

Cheers

 

Stu

Link to comment
https://forums.phpfreaks.com/topic/296989-arrays/
Share on other sites

If you are "using mySql and a loop to generate each array" how are you ending up with the string $arrayOutSideTempData?

 

Probably better if you post the code so we can see what you are doing (use the <> button in the toolbar to post code)

Link to comment
https://forums.phpfreaks.com/topic/296989-arrays/#findComment-1514736
Share on other sites

Sorry, here you go....

$arrayOutSideTempData = "";
while($rowSensor = mysqli_fetch_assoc($resultSensor)) {
  $TimeSamp = $rowSensor["sensorDBTimeStamp"];
  $OutsideTemp =  $rowSensor["sensorOutsideTemp"];
  $arrayOutSideTempData = $arrayOutSideTempData . "array('".$TimeSamp."', ".$OutsideTemp."),";
}
			
$arrayOutSideTempData = rtrim($arrayOutSideTempData, ","); // remove the outstanding , from the end

Cheers

 

Stu

Link to comment
https://forums.phpfreaks.com/topic/296989-arrays/#findComment-1514737
Share on other sites

Thats not how you define an array in PHP all you are doing is defining a string.

 

You should be able to do something like

$arrayOutSideTempData= array();
while(...)
{
   ...
   $arrayOutSideTempData[] = array($TimeSamp, $OutsideTemp); // add $Timestamp, $outsideTemp values as an array to $p->data array
}

$p->data = array($arrayOutSideTempData);

You can then use  print_r($p->data);  after the loop to check the array structure

Link to comment
https://forums.phpfreaks.com/topic/296989-arrays/#findComment-1514738
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.