Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

var data = <?php echo json_encode($result_array); ?>;
share|improve this answer
    
You are a saint and a scholar. Darwin award for dumbest developer goes to me. (Thanks) –  spm Jan 29 '13 at 2:55
    
@spm Accept this answer by clicking the checkmark? –  Daedalus Jan 29 '13 at 2:58
    
Welcome back Daedalus, stack overflow won't let me yet. –  spm Jan 29 '13 at 3:01
    
@spm Purpose of 'welcoming me back', when I've only been here once? –  Daedalus Jan 29 '13 at 4:40
    
You've commented on one of my posts before, lol. –  spm Jan 29 '13 at 7:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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