$data_array = [
["","","","2",""],
["","1","","",""]
];
$myJSONString = json_encode($data_array);
$my = [
"id"=> "1",
"name" => "easy",
"data" => $myJSONString,
];
$myJSON_File->add($my);
我想在 JSON 中存储一个矩阵,然后检索它以便稍后将其发送到 JavaScript 函数。我尝试过,但是当我将其发送到 JS 函数时,矩阵会作为字符串发送。我还尝试解码它 $myArray = json_decode($output);
但这也不起作用。
最佳答案
将其作为字符串“发送”的方式是正确的,您只需解析字符串即可获取要像这样使用的 javscript 对象/数组。
使用json_encode获取php文件中的字符串。
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);
关于javascript - 使用 PHP 从 JSON 检索数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59698221/