将php数组传递给javascript数组

时间:2014-04-26 01:45:37

标签: javascript php arrays

我正在寻找将php数组传递给javascript的最佳方式。

我正在制作RPG,当他们登录时,我想从数据库返回他们保存的数据并存储在javascript端的数组中:

要获取数据,我这样做:

$.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数组的最佳方法吗?

2)为什么我无法使用game.data.dataArray访问game.data.dataArray[0]的某个数据元素。

这些提供1: 120,mymap2

的相同输出
        log("1: " + game.data.dataArray);
        log("2: " + game.data.dataArray[0]);

returned_data.split(" ")是否应该将返回的字符串拆分为两个数组元素?


编辑:好的,我已完成echo json_encode($message);并返回引号,但仍会返回game.data.dataArraygame.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 

2 个答案:

答案 0 :(得分:2)

您需要做的是,更改此代码:

while ($row = $result->fetch_assoc()) {
    $message =  $row['experience'] . $row['levelname'];
}

将其作为数组:

while ($row = $result->fetch_assoc()) {
    $message[] =  $row['experience'] . $row['levelname'];
}

所以,$message现在变成了一个数组。您需要将此输出到客户端。您可以使用以下方式执行此操作:

die(json_encode($message));

原因是,在您的代码中,它只会输出最后一个。

答案 1 :(得分:2)

如果您使用$.getJSON并提供有效的json,则无需解析任何内容,因此您可以将php更改为:

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:

$.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`
    }
);