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

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -