将php数组传递给javascript数组 [英] pass php array to javascript array
问题描述
我正在寻找将php数组传递给javascript的最佳方法。
I'm looking for the best way to pass php array to javascript.
我正在制作一个RPG,当他们登录时,我想从数据库返回他们保存的数据并存储在javascript端的数组中:
I'm making a RPG, and when they login, I'd like to return their saved data from database and store in an array on javascript side:
要获取数据,我这样做:
To get data, I do:
$.getJSON("php/CRUD.php", {"_functionToRun" : ""},
function (returned_data) {
game.data.dataArray.push(returned_data.split(" "));
log("1: " + game.data.dataArray); //output: `1: 120,mymap2`
log("2: " + game.data.dataArray[0]); //output: `2: 120,mymap2`
}
);
PHP:
$qry =
'SELECT *
FROM userstats
WHERE id_user_fk ="' . $_SESSION['userid'] . '"
LIMIT 1';
$result = $mysqli->query($qry) or die(mysqli_error($mysqli));
while ($row = $result->fetch_assoc()) {
$message = $row['experience'] . $row['levelname'];
}
echo json_encode($message);
1)这是将一组值放入js数组的最佳方法吗?
1) Is this the best way to get a set of values into a js array?
2)为什么我不能使用访问
。 game.data.dataArray
的某个数据元素game.data.dataArray [0]
2) Why can't I access a certain data element of game.data.dataArray
using game.data.dataArray[0]
.
这些产生的输出相同 1:120,mymap2
log("1: " + game.data.dataArray);
log("2: " + game.data.dataArray[0]);
应该 returned_data.split()
将返回的字符串拆分为两个数组元素?
Should returned_data.split(" ")
split the returned string into two array elements?
编辑:好的我已经完成 echo json_encode ($ message);
并返回引号,但仍返回 game.data.dataArray
和游戏的相同结果.data.dataArray [0]
Okay I've done echo json_encode($message);
and it returns with quotes, but still returns same results for game.data.dataArray
and game.data.dataArray[0]
1: "120,mymap2"
2: "120,mymap2"
我还将功能更改为 $ .getJSON
再次更改为
$qry =
'SELECT *
FROM userstats
WHERE id_user_fk ="' . $_SESSION['userid'] . '"';
$result = $mysqli->query($qry) or die(mysqli_error($mysqli));
while ($row = $result->fetch_assoc()) {
$array[] = $row['experience'] . $row['levelname'];
die(json_encode($array[]));
}
和JS是:
$.getJSON("php/CRUD.php", {"_functionToRun" : ""},
function (returned_data) {
game.data.dataArray.push(returned_data.split(" "));
log("1: " + game.data.dataArray);
log("2: " + game.data.dataArray[0]);
}
);
输出:
Uncaught melonJS: level <br />
<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-...<omitted>...nd
推荐答案
如果您使用 $。getJSON
并且您提供了有效的json,则无需解析任何内容,因此您可以将php更改为:
If you use $.getJSON
and you provide valid json, you don't need to parse anything, so you can change your php to:
if ($row = $result->fetch_assoc()) {
echo json_encode($row);
exit; // You're limiting the number of rows to 1, so there is no need to continue
}
和javascript:
And the javascript:
$.getJSON("php/CRUD.php", {"_functionToRun" : ""},
function (returned_data) {
// you can push `returned_data` to an array / add it to an object,
// but then you need to keep track of its index
log("1: " + returned_data.experience); //output: `1: 120`
log("2: " + returned_data.levelname); //output: `2: mymap2`
}
);
这篇关于将php数组传递给javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!