Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
184
rated 0 times [  189] [ 5]  / answers: 1 / hits: 15124  / 15 Years ago, mon, february 1, 2010, 12:00:00

it was always a question for me that how can i use an array value in javascript while that array is defined in my php scripts



For example consider reading some values from a file and use it in javascript.



what's ur plan to do so ?


More From » php

 Answers
24

You can use the json_encode function, to safely return a JSON object which you can use directly in JavaScript:



<?php
$phpArray = array(foo, bar, baz);
//....
?>

<script type=text/javascript>
var jsArray = <? echo json_encode($phpArray); ?>;
</script>


Outputs:



<script type=text/javascript>
var jsArray = [foo,bar,baz];
</script>

[#97690] Friday, January 29, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaycie

Total Points: 414
Total Questions: 96
Total Answers: 117

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
;