Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

This question already has an answer here:

I get array from php,and then use to change js array and display but its show all elements as an array.##

$name = array('A','B','C','D');

<script> 
     <?php echo "var name='".json_encode($name)."';";   ?>
     for (var i in name){
                        alert(name[i]);
                         }
</script>
share|improve this question

marked as duplicate by procrastinator, Dagon php Jun 17 at 4:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Many duplicates on SO, many results in Google :-| – procrastinator Jun 17 at 4:06

2 Answers 2

up vote 1 down vote accepted
<script type='text/javascript'>
<?php
$name = array('A','B','C','D');
$js_array = json_encode($name );
echo "var javascript_array = ". $js_array . ";\n";
?>
</script>

Reffer this Answer

Note json_encode() is only available in PHP 5.2 and up

share|improve this answer

No need to wrap the value in '', if you wrap it the value will be considered as a string not an array in javascript

<?php echo "var name=".json_encode($name).";";   ?>
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.