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 ?
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 ? |
|||
|
You can use the
Outputs:
|
|||
|
Something like this?
And perhaps for more info: |
|||||
|
JSON is your choice, since some PHP 5.x version, PHP contains a function
As usual, some nice guys wrote json_encode() functions for older PHP version, checkout the comments on php.net. |
|||
|
The array in your PHP needs to be exposed to JavaScript in some way. If you want the array available to JS on the initial page load, you might do it like this:
If the variable doesn't need to be created on the initial page load, you can do something similar, but use an AJAX call. Simply have your PHP script respond with the array formatted for JS, and store the result of the call in your JS variable. |
|||
|
Variant on the PHP to JS without json extension, using join / implode and no loop construct:
|
|||
|