如何将一串数字转换为数组

How to convert a string of numbers to an array

本文关键字:数字 一串 转换 数组      更新时间:2024-03-08

我有一个很大的JavaScript数组,整数在-10到10之间。我想把它们保存在数据库中,所以我必须使用jQuery$.post函数将它们发送到PHP;但是,数组太大了,所以我用JavaScript将数组转换为字符串。我想用PHP把它转换回一个整数数组。我该怎么做?

这是我的代码,到目前为止:

function updateDb(){
for(i = 0; i < inputSquares.length; i++) {
    for(j = 0; j <= 9; j++) {
        perceptronsWeights[10*i+j] = inputSquares[i].weights[j]
    };
};
var pws = String(perceptronsWeights);
alert(typeof pws);
$.post("updateDatabase.php", {size: size, weights:pws}).done (
    function(data) {
        alert (data);
    }
)

在我的PHP文件中,我使用:

$weightsString = $_POST['weights'];
$pws = array_map("intval", explode(",", $weightsArray));
echo $pws;

我也尝试过没有array_mapstr_splitexplode(),但它们都给了我这个错误:

注意:
中的数组到字符串的转换C: ''examplep''htdocs''ANN_JS''updateDatabase.php,第19行
阵列

第19行为echo $pws;

使用print_rvar_dump来回显$pws的数组详细信息。如果你想通过echo,那么试试

`echo implode(" ",$pws);`

使用print_r($pws)打印array