stownsend Posted June 23, 2015 Share Posted June 23, 2015 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted June 23, 2015 Share Posted June 23, 2015 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) Quote Link to comment Share on other sites More sharing options...
stownsend Posted June 23, 2015 Author Share Posted June 23, 2015 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 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted June 23, 2015 Share Posted June 23, 2015 (edited) 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 June 23, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
stownsend Posted June 24, 2015 Author Share Posted June 24, 2015 Thanks for the help, before creating the array had to amend the $OutsideTemp var to intvar. Spot on, thanks ! Cheers Stu Quote Link to comment Share on other sites More sharing options...
Barand Posted June 24, 2015 Share Posted June 24, 2015 But your temperature values are not int (eg 21.06, 19.69) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.