spm
spm

Reputation: 131

json encoded php array returns undefined in javascript

This is a follow up to a previous post.

I'm attempting to pass some data to javascript via json_encode.

I thought I would be able to put the data in to a javascript array as follows:

    var data = [<?php echo json_encode($result_array); ?>];

But when I try to call values they return as undefined. I imagine that it might be putting the data as a singular string inside the array.

If anyone can provide advice that would be great. Thanks a lot!

Upvotes: 0

Views: 771

Answers (1)

xdazz
xdazz

Reputation: 160833

If $result_array is an array in php, then you just need to do:

var data = <?php echo json_encode($result_array); ?>;

Upvotes: 2

Related Questions