Home » Javascript & Libraries, PHP/MySQL » Convert PHP array to Javascript array

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>

22 Comments

  1. JavaScript Examples says:

    that’s all cool & great js tips for beginners, thank you very much for sharing.

  2. Saira says:

    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

  3. Aneeska says:

    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.

  4. jm says:

    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 :D

  5. Iranian says:

    it was so cool thank you 100000000000000000000000000000000 times

  6. anuj says:

    Thanks for your support…

  7. Sunrise says:

    I agree, Jm! it’s so simple. smart! thanks Aneeska. It help me very much… thanks…

  8. Archana says:

    Thanks
    short simple & very sweet.

  9. A says:

    Thanks mate. Really appreciate your help. Its been very helpful.
    Cheers

  10. 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] + "” );
    }
    }

  11. Thanks a lot for the code.

  12. great code! now im upto next phase of my program. thanks

  13. Den says:

    Hi everyone! sorry! i’m very beginner in programing but it’s like a hobby and i like learning. For now i’m trying to make a google maps like map (OpenStreetMap) and trying to learn how to load the array from php not using(for now) JSON and AJAX(cause i’m far from it). So I have a php array with coordinates and some other staff. Also i have checkboxes on page which are restaurants, museums e.t.c. With js by the ‘for’ cicle i’m working with checkboxes but i can’t connect the value from checkbox with the php array’s name and load it. tell me please what’s wrong?

    array (“inform.kg”,”74.61920″,”42.83772″,”ugfjfh”,”2″),
    2 => array (“inform.kg2″,”74.61930″,”42.83762″,”ugfh”,”2″)
    );
    ?>

    function checkBox(){
    var checkBoxes = document.getElementsByTagName(‘input’);
    for(var i=0; i<checkBoxes.length; i++){
    if (checkBoxes[i].id == 'check'){
    if(checkBoxes[i].checked == true){
    //logTest(checkBoxes[i].name)
    var obj_name = checkBoxes[i].value;
    var objs = [""];;
    logTest(objs)
    }
    }
    }
    }

    <input name="internet" id="check" onclick="checkBox()" value="” type=”checkbox”>inform agency
    <input name="internet" id="check" onclick="checkBox()" value="” type=”checkbox”>inform agency2

  14. den says:

    ”var objs = [""];;” so instead of “$internet” i wan’t to pass another variable (JS Array) to work with it! help me please! :)

  15. den says:

    deam, sorry, how can i write heare the php variable with $ ?

  16. den says:

    var objs = [""];

  17. den says:

    how can i insert the code here to show it correctly? like in the first post?

  18. bhawin says:

    this is a very great code , keep it up buddy.
    thnx for posting

Leave a Reply

Page optimized by WP Minify WordPress Plugin