初学 ajax ,从数据库里捞数据,用 responseText 实现后,都转换为了 string ,可以转回 array 么?
PHP 取数据代码:
-$data 中是需要的数据,是一个 array
<?php
$conn=mysql_connect("localhost","root","root");
if(!$conn){
echo "连接失败";
}
mysql_select_db("gold_price",$conn);
mysql_query("set names utf8");
$sql="select * from `gold_price`";
$res=mysql_query($sql,$conn);
//print_r(mysql_fetch_array($res));
$j = 0;
while($row=mysql_fetch_row($res)){
$data[$j] = $row[2];
$date[$j++] = $row[1];
}
print_r($data);
?>
-----------------------------------------------------------------------------------------------------
javascript 部分
function byphp(){
var byphp = xmlhttp.responseText;
alert(byphp);
}
然后 byphp 就成了 string
眼睁睁看着结果是下面这样,就是没法用 array 操作数据,好心塞。
Array
(
[0] => 272.95
[1] => 272.95
[2] => 272.95
[3] => 270.20
[4] => 268.00
[5] => 270.40
[6] => 270.40
[7] => 270.80
[8] => 270.80
[9] => 267.30
[10] => 267.30
[11] => 267.50
[12] => 267.60
[13] => 267.75
[14] => 267.40
[15] => 267.40
[16] => 268.50
[17] => 270.75
[18] => 270.75
[19] => 269.60
[20] => 268.70
)
PHP 取数据代码:
-$data 中是需要的数据,是一个 array
<?php
$conn=mysql_connect("localhost","root","root");
if(!$conn){
echo "连接失败";
}
mysql_select_db("gold_price",$conn);
mysql_query("set names utf8");
$sql="select * from `gold_price`";
$res=mysql_query($sql,$conn);
//print_r(mysql_fetch_array($res));
$j = 0;
while($row=mysql_fetch_row($res)){
$data[$j] = $row[2];
$date[$j++] = $row[1];
}
print_r($data);
?>
-----------------------------------------------------------------------------------------------------
javascript 部分
function byphp(){
var byphp = xmlhttp.responseText;
alert(byphp);
}
然后 byphp 就成了 string
眼睁睁看着结果是下面这样,就是没法用 array 操作数据,好心塞。
Array
(
[0] => 272.95
[1] => 272.95
[2] => 272.95
[3] => 270.20
[4] => 268.00
[5] => 270.40
[6] => 270.40
[7] => 270.80
[8] => 270.80
[9] => 267.30
[10] => 267.30
[11] => 267.50
[12] => 267.60
[13] => 267.75
[14] => 267.40
[15] => 267.40
[16] => 268.50
[17] => 270.75
[18] => 270.75
[19] => 269.60
[20] => 268.70
)