$data_array = [
["","","","2",""],
["","1","","",""]
];
$myJSONString = json_encode($data_array);
$my = [
"id"=> "1",
"name" => "easy",
"data" => $myJSONString,
];
$myJSON_File->add($my);
I want to store a matrix in JSON and then retrieve it to send it later to a javascript function. I tried but when I send it to JS function, the matrix is sent as a string. I also tried decoding it $myArray = json_decode($output); but that didn't work either.
1 Answer 1
The way of "sending" it as a string is correct, you simply have to parse the string to get a javscript object / array to work with like this.
Use json_encode to get a string in the php file.
let string_example = '{"someVariable":"123"}'; //the string you retrieve from php
console.log(string_example);
let retrieved_obj = JSON.parse(string_example);
console.log(retrieved_obj.someVariable);
answered Jan 11, 2020 at 20:12
Aaron
1,7471 gold badge10 silver badges18 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default