Pass php array values as JavaScript array values -


i have php array this.

array (      [0] => hal([[br,1],[cl,4]])   [1] => alk([[nc(1),1,2],[nc(1),3]]) ) 

how pass javascript below.

var hal=[["br",1],[cl,4]]; var alk=[[nc(1),1,2],[nc(1),3]]; 

i write code

<script>   var data = <?=json_encode($input);?>;    //$input name of php array   var hal=[data[0].substring(5,data[0].lastindexof(']'))];   var alk=[data[1].substring(5,data[1].lastindexof(']'))];   document.write(hal[0]); </script> 

the output [br,1],[cl,1] , expected output 1 below.any ideas? thank you.

document.write(hal[0]);  => ["br",1] document.write(hal[0][0]); => ["br"] 

if want multiple variables, you'll want loop through array; can grab names using regular expression. if you're trying turn valid data can parse, json string, you're going have awful lot of work; wherever you're getting string better place to. have them pass valid json string instead.

<script> <?php foreach($input $v) {     preg_match("/(\w+)\((.*)\)/", $v, $matches);     $var = $matches[1];     $val = str_replace("'", "\\'", $matches[2]);     echo "var $var = '$val';\n"; } ?> </script> 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -