php - Convert json to array in jquery -


i'm trying convert json object array. i've saw many questions on didn't me.

my php code :

$arr = array ( [1] => 10 [5] => 20 ) //array key random 

i want assign above array jquery variable.

jquery code :

var obj = '<?php echo json_encode($arr)?>'; 

when print obj gives me {"1":"10","5":"20"}. want result in array [1:10,5:20].

and after want access array values key (e.g. obj[1] or obj[5])

ignore syntax error.

thanks.

you need define var obj without usage of ' in script

<script>  var obj = <?php echo json_encode($arr)?>; alert(obj[1]);// alert 10  </script> 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -