samedi 21 février 2015

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;.





Aucun commentaire:

Enregistrer un commentaire