1

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>
1
  • 1
    Many duplicates on SO, many results in Google :-| Commented Jun 17, 2015 at 4:06

2 Answers 2

1
<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

Sign up to request clarification or add additional context in comments.

Comments

1

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).";";   ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.