A very basic tutorial. For the beginners who want to pass array values from PHP to Javascript for them to be accessible in the client side JS as an array. It’s really easy to pass PHP array to Javascript.
The below code passes a string array in PHP to Javascript.
<?php
$arr = array("a","b","c");
?>
<script>
var jsArray = ["<?php echo join("\", \"", $arr); ?>"];
alert(jsArray);
</script>



that’s all cool & great js tips for beginners, thank you very much for sharing.
Hi
I have an array that contains database records. This array is displayed as a table. Then I have to give an option for downloading this table as .xls file. For this purpose, I am trying to incorportae javascript to call another php page where this table is converted to string and printed in excel file. So, how can i pass a php array to javascript array and pass it to another page through url?????????
I ll be very grateful, if u can help me…
Thanks in advance
If u have some better idea for this task then please let me know
1. If I understand your problem correctly, I believe you want to display a tabular data in your web page and then allow your users to download the same data as an excel file. You don’t need to bring in Javascript if this is what you need to achieve. Query your database table and display them on your page in a table. When user clicks on the download link, you will call another php page which will again query the same database as above and generate the content for the excel file and give it back to the user.
2. If you still want to pass the PHP array to Javascript and then pass it to another page through Javascript, you should not pass the data through URL. Convert your PHP array to a JSON string and store it in a variable in Javascript. Then, when you want the Javascript data to be sent to another page, you can create a POST request from Javascript to the second PHP page to send the JSON data. PHP can then decode this string in to an Array.
Above are two concepts. If I am clear and you are able to choose the way you want to go ahead, I can elaborate the solution.
Thank you very much for this.:D
I’ve been through so many forum threads regarding conversion between php and javascript arrays and this is the simplest yet the most effective one i’ve found.Again. thanks
it was so cool thank you 100000000000000000000000000000000 times
Thanks for your support…
I agree, Jm! it’s so simple. smart! thanks Aneeska. It help me very much… thanks…
Thanks
short simple & very sweet.
Thanks mate. Really appreciate your help. Its been very helpful.
Cheers
php code =========
//create php test array
$testarray = array(‘zero’, ‘one’, ‘two’, ‘three’, ‘four’);
//convert array to string using “:#:” as separator
//using complex separator insures that it is not part of array element
$testvar = implode(“:#:”,$testarray);
php code ends =========
function showArray()
{
//Convert Php string to JavaScript string
var testvar = “”;
//Create JavaScript array
var testarray = new Array();
var x;
//Fill JavaScript array from the converted string
testarray = testvar.split(“:#:”);
//Display output of all the array elements;
for( x=0; x<5; x++){
document.write( testarray[x] + "” );
}
}