-1

Possible Duplicate:
PHP : Create array for JSON

I have an array like this :

Array ( [0] => tag1 [1] => tag2 [2] => tags17c [3] => tags20 [4] => tags21)

I want put that array on javascript, and the format must be like this :

var Tag = ['tag1', 'tag2', 'tags17c', 'tags20', 'tags21'];

How to convert it? please, help me out..

Community
  • 1
  • 1

2 Answers2

6
var Tag = <?php echo json_encode(array_map("htmlspecialchars", $your_array)); ?>;
3

My advice would be to use json for that, as Charly suggested, using json_encode. Though there is no real reason for using array_values() as far as I know,

var Tag = <?php echo json_encode($array); ?>;
Johan
  • 1,537
  • 1
  • 11
  • 17
  • If the array is an associative array, then a JSON object will be created. –  Aug 01 '11 at 07:37