简体   繁体   中英

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; however, the array is too big, so I converted the array to a string in JavaScript. I want to convert it back to an array of integers in 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:

$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:

Notice: Array to string conversion in
C:\\xampp\\htdocs\\ANN_JS\\updateDatabase.php on line 19
Array

Line 19 is echo $pws; .

使用print_r($pws)打印array

use print_r or var_dump for echoing the array details for $pws. If you want through echo then try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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