Add double quotes in JavaScript array passed from php

Hi,

I am adding values in array using array_push and separating array using comma (,).

Now I want to pass this array to javascript with double quotes otherwise I am getting error as “Uncaught SyntaxError: Unexpected token :”

<?php

$stack = array();
$stack2 = array();
		
array_push($stack, "value1");array_push($stack, "value2");
array_push($stack2, "value3");array_push($stack2, "value4");
			
$array = implode(',', $stack);
$array2 = implode(',', $stack2);
		
?>
<script type="text/javascript">

var array1=[value1,value2];
var array2=[value3,value4];

/*expected values*/
var array1=["value1","value2"];
var array2=["value3","value4"];

</script>

Any Idea?

-Thanks

Hi edge, welcome to the forum

should be ??

var array1=["value1","value2"];
var array2=["value3","value4"];
1 Like

Why not use single quotes around the values in JavaScript - unlike PHP single and double quotes have the same meaning in JavaScript.

I’d go for json_encode() rather than implode().

Many thanks friends.

json_encode solution works :smile:

Yes I am agree felgall we can use single or double in java script.

-Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.