What is the best way to remove double quotes from strings set using MySQL _FETCH?
I have a php script to retrieve data from a MysQL databse following an ajax call. The data needs to be returned to the calling web page as a string representing a series of x,y values to be displayed on a scatter graph.
The required format which the php script needs to output to the calling page (which will pick it up from xmlhttp.responseText) is as follows:
[[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7]]
What I am getting is:
[[0,"69.08"],[1,"53.53"],[2,"61.61"],[3,"72.04"],[4,"82.72"],[5,"103.76"]]
The PHP code I am using to build the return string is:
while ($row=mysql_fetch_array($res_Data,MYSQL_NUM)) {
$index[$i] = $i;
$PointArray[0] = $i;
$PointArray[1] = $row[1];
$ReturnData[$i] = $PointArray;
$i = $i + 1;
}
echo json_encode($ReturnData);
Interestingly, when I echo the data to a html table displayed on the calling page rather than setting it as a php array, no double quotes appear.
My questions is twofold:
(1) Is there a more elegant way o开发者_如何学编程f building the data string than what I have come up with which appears a bit clumsy?
(2) What's the best way to remove the double quotes - should this be done server side or in the javascript function on the web page initiating the call?
Many thanks
If you don't want quotes to appear - just don't put a strings in array ;-)
Replace your code to something like (I added (float)
modifier before $row[1]
):
while ($row=mysql_fetch_array($res_Data,MYSQL_NUM)) {
$index[$i] = $i;
$PointArray[0] = $i;
$PointArray[1] = (float)$row[1];
$ReturnData[$i] = $PointArray;
$i = $i + 1;
}
Or a little more compact:
while ($row=mysql_fetch_array($res_Data,MYSQL_NUM)) {
$index[$i] = $i; // what is it used for?
$ReturnData[$i] = array($i, (float)$row[1]);
$i++;
}
精彩评论