Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP array to JavaScript array conversion

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 357
    Comment on it

    Sometimes, I feel a need to get a PHP array in my JavaScript code. Earlier I make use of implode function of php but recently found new way using json_encode that works very effectively.

    Here is my php array:

    1. <?php
    2. $cool_epl_clubs_php = array('Manchester United', 'Arsenal', 'Liverpool', 'Chelsea', 'Everton', 'Swansea');
    3. ?>

    Here is a way to get JavaScript array which have these values

    1. <script type='text/javascript'>
    2. <?php
    3. $js_encoded_array = json_encode($cool_epl_clubs_php);
    4. echo "var coolEplClubsJs = " . $js_encoded_array . ";\n";
    5. ?>
    6. </script>

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: