我刚刚使用硬编码数组完成了一个脚本工作:
dataArray[0] =[50,10,0.3,0.25,50,"FFF",3];
dataArray[1] =[50,10,0.3,0.2,50,"FFF",3];
....
dataArray[5] =[50,20,0.5,0.7,120,"FF0",4];
我将dataArray的内容放入db表中,因此我可以删除硬编码数组 我正在使用PHP从MySql返回字符串中的数据,并且数据转储显示值的数量和值本身是正确的。
但是,我显然没有正确编码数据捕获,因为我最终没有像我的硬编码数组那样的数组(我需要维护我的硬编码数组的结构)。
这是db read的代码:
function getSomeData(){
$.ajax({
type: "GET",
url: "GetSomeData.php",
dataType: "text",
success:function(result){
setSomeData(result);
}
});
}
var someDataArray = new Array();
function setSomeData(resultData){
var resData = resultData.split('^');//record split
for(var i = 0; i < resData.length; i++){
someDataArray[i] = resData[i].split('#');//field split
someDataArray[i].pop();//removes array element occupied by '^'
if(i == resData.length - 1){
setDataArray();
}
}
}
setDataArray()中的for循环不会创建正确的数组结构,因为someDataArray [x]只是一个字符串:
var dataArray = new Array();
function setDataArray(){
for(var i = 0; i < dataArray.length; i++){
dataArray[i] = someDataArray[i];
}
}
所以我尝试将someDataArray [x]放在一个数组中,如下所示:
dataArray[i] = [someDataArray[i]];
但这也不起作用。
我花了两天的时间试图解开这个问题,阅读博客,尝试我能想到的一切,但没有运气。我认为这是一个简单的解决方案,但我无法得到它。
帮助?
修改
在学习了一些关于JSON和json_encode之后,我现在让我的脚本工作了。我想发布我这样做的方式来承认我从Pat Burke那里得到了一些宝贵的建议。下面的代码可能不是他的想法,但我按摩它直到它工作。但我真的不明白它为什么会起作用,所以我想在json_encode上做更多的阅读。
请注意,在ajax调用中使用dataType:“json”会引发错误。
// GetSomeData.php
$return_arr = array();
$query1 = "SELECT * FROM mytable ORDER BY idx ASC";
$result = mysql_query($query1) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($result)){
$resultArray = array();
$resultArray[] = (int)$row['dc'];
$resultArray[] = (int)$row['smlspc'];
$resultArray[] = (float)$row['sclx'];
$resultArray[] = (float)$row['scly'];
$resultArray[] = (int)$row['lgspc'];
$resultArray[] = (int)$row['colr'];
$resultArray[] = (int)$row['diam'];
if(count($resultArray) == 7){
array_push($return_arr, $resultArray);
}
}
echo json_encode($return_arr);
mysql_free_result($result);
// new js
function getSomeData(){
resultData = new Array();
$.ajax({
type: "GET",
url: "GetSomeData.php",
dataType: "text",
//dataType: "json", //using this threw an error (see below)
success:function(result){
resultData = $.parseJSON(result);
$("#p1").append("resultData.length =" + resultData.length + "<br />");
//resultData.length =114 (it's a string not an array)
$("#p1").append("resultData =" + resultData + "<br />");
//resultData =
//[[50,10,0.375,0.25,50,0,0],
//[50,10,0.3,0.2,50,0,1],
//[50,10,0.6,0.4,0,0,2],
//[50,0,0.4,0.4,0,0,3],
//[50,0,0.4,0.4,0,0,3]]
for(var i = 0; i < resultData.length; i++){
$("#p1").append("resultData[" + i + "] =" + resultData[i] + "<br />");
//data displayed with dataType: "text"
//resultData[0] =50,10,0.375,0.25,50,0,0
//resultData[1] =50,10,0.3,0.2,50,0,1
//resultData[2] =50,10,0.6,0.4,0,0,2
//resultData[3] =50,0,0.4,0.4,0,0,3
//resultData[4] =50,0,0.4,0.4,0,0,3
//data displayed with dataType: "json"
//resultData is null
}
},
//this has no effect with either dataType: "text" or dataType: "json"
contentType: 'application/json'
});
}
答案 0 :(得分:0)
如果someDataArray [i]是由逗号分隔的值字符串,则可以执行
dataArray [i] = someDataArray [i] .split(&#39;,&#39;)