JS array to PHP -


i have string containing javascript array next way:

$array = "var array = [ { 'a' : 'val1', 'b': 1}, { 'a' : 'val2', 'b': 2} ];"; 

how can convert string php array in next structure:

$array[0] => array('a' => 'val1', 'b' => 1) $array[1] => array('a' => 'val2', 'b' => 2) 

thanks

you should using json communicate code between php , js. however, don't know want use code for, want (as general rule, don't want use that):

<?php $str = 'var array = [ {"a": "val1", "b": 1}, {"a": "val2", "b": 2} ];'; $matches = array(); preg_match("/^(var\s+)*([a-za-z0-9_\.]+)\s*=\s*([^;]+);$/", $str, $matches); print "<pre>"; var_dump($matches); print "</pre>"; $array = json_decode($matches[3], true); print "<pre>"; var_dump($array); print "</pre>"; ?> 

also note had replace single quotes double quotes work, have no idea why had that.

if why need this, might little more help.


Comments

Popular posts from this blog

JQuery Autocomplete without using label, value, id -

c++ - Accessing inactive union member and undefined behavior? -

JAVA - what is the difference between void and boolean methods? -