0

Background Information

I need to "pass" a javascript array to some front end logic that someone else wrote. in reviewing their code, I see they are creating dummy data like this:

<script>
//define some sample data
 var tabledata = [
    {id:1, name:"JohnDoe", company:"Acme", email:"[email protected]", title:"CEO", regtype:"12345"},
    {id:2, name:"JaneDoe", company:"Widgets Inc", email:"[email protected]", title:"Programmer", regtype:"12345"}    
 ];

My logic is all the backend stuff that gets real data from a database. I get back a json string that looks like this:

{
    "items": [{
        "id": 2089025,
        "first_name": "Devon",
        "last_name": "Ackerman"
    }, {
        "id": 2163367,
        "first_name": "Ryan",
        "last_name": "Acuff"
    }]
}

Problem

I don't know the correct way to convert/cast this in PHP to make it end up as an array in Javascript.

Code

This is the code I have so far in PHP that a) proves I'm getting a string b) tries to convert it to a json object.

$json_obj = json_decode($response, true);
//echo json_last_error();
echo gettype($response);
echo gettype($json_obj);

The first gettype() returns "string". The second returns "array".

A few lines later, i finish the PHP section and I do this:

<script type="text/javascript">
// pass PHP variable declared above to JavaScript variable
var tabledata = <?php echo ($json_obj) ?>;
console.log(typeof(tabledata))
console.log(tabledata)

I see the following in the console as the output for the two calls to console.log():

function
ƒ Array() { [native code] }

can someone point me in the right direction?

Thanks.

  • json_decode turns JSON text into a PHP object. Assuming what you get from the database is an associative array, you need to call json_encode on it. – Chris G 11 hours ago
  • As stated don't decode, just assign: var tabledata = <?php echo $response ?>; – AbraCadaver 11 hours ago
  • ok except that there's an "items" array embedded in $response. So I just want the stuff inside items. – dot 11 hours ago
1
var tabledata = <?php echo ($json_obj) ?>;

Should be...

var tabledata = <?php echo json_encode($json_obj) ?>;

This will turn it into json, and the browser will auto parse it for you when the page loads.

In your case, since you already started with an encoded value, you could just use that.

var tabledata = <?php echo $response ?>;

Or I believe PHP has a shortcut for echoing that you could also do...

var tabledata = <?= $response ?>;
  • Since they are decoding $response (JSON string) why not just echo that? – AbraCadaver 11 hours ago
  • They could do that also. However in case there is a future example that they are working with something that does not start off with an already encoded value, this will let them know how to do that. – Taplar 11 hours ago
  • var tabledata = <?= json_encode($json_obj, JSON_PRETTY_PRINT) ?>; is what I would do – David Wyly 11 hours ago
  • how can i get access to just the "items" section? ive tried $json_obj->items and also $json_obj=>items. – dot 11 hours ago
  • 2
    I think I got the answer. I need to do json_encode($json_obj['items']) – dot 10 hours ago

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.