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
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
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

Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.