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
    • 323
    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:

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

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

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

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: