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)