简体   繁体   English

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

[英]How to convert a string of numbers to an array

I have a pretty big JavaScript array with integers between -10 and 10. I want to save those in a database, so I have to send them to PHP using the jQuery $.post function; 我有一个非常大的JavaScript数组,整数介于-10和10之间。我想将它们保存在数据库中,所以我必须使用jQuery $ .post函数将它们发送到PHP; however, the array is too big, so I converted the array to a string in JavaScript. 但是,数组太大,所以我将数组转换为JavaScript中的字符串。 I want to convert it back to an array of integers in PHP. 我想将它转换回PHP中的整数数组。 How can I do this? 我怎样才能做到这一点?

This is my code, so far: 这是我的代码,到目前为止:

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);
    }
)

And, in my PHP file, I use: 并且,在我的PHP文件中,我使用:

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

I have also tried explode() without array_map and str_split , but both of them give me this error: 我也试过没有array_mapstr_split explode() ,但是它们都给了我这个错误:

Notice: Array to string conversion in 注意:数组转换为字符串
C:\\xampp\\htdocs\\ANN_JS\\updateDatabase.php on line 19 第19行的C:\\ xampp \\ htdocs \\ ANN_JS \\ updateDatabase.php
Array 排列

Line 19 is echo $pws; 第19行是echo $pws; .

使用print_r($pws)打印array

use print_r or var_dump for echoing the array details for $pws. 使用print_rvar_dump来回显$ pws的数组细节。 If you want through echo then try 如果你想通过echo然后尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM