1

I have a multidimensional array, here:

$noticeDate = json_encode( $noticesDates );

and I want to pass the array into javascript:

var unavailableDates[] = $noticeDate;

Both variables are in the same php file so there is little point using $.getJSON, which basically looks for the variable in an external file. However, how do I pass the object into the javascript array in the same script.

Cheers

3 Answers 3

4

You cant directly assign php variables to js, but you can use something like that:

<script>
  var unavailableDates = jQuery.parseJSON('<?php echo json_encode($noticeDates) ?>');
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

@Christoph's solution lacks the client-side JSON parsing that is included here. This is the more complete solution.
var unavailableDates = jQuery.parseJSON('<?php echo json_encode($noticesDates) ?>'); document.write(unavailableDates); doesnt seem to print it out
Why do you want to print it out? It's an object you can't just simply print it. If that's your goal you should use a different format imho.
1

use this

var array = JSON.parse("<?php echo json_encode($noticesDates) ?>");

Comments

0

Try this one: $.pareseJSON()

here is example:



var json = "<?php echo json_encode($noticesDates); ?>";

jsArray = jQuery.parseJSON(json);

1 Comment

How does this even attempt to answer the question? Please change your answer to something more resembling one.

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.