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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.