0

我想从查询中获取 PHP 变量

?q=aaa

并根据关联数组在JS中定义一个变量:

{
    "aaa": "111",
    "bbb": "222",
    "ccc": "333"
};

所以如果

var oldVariable = '<?php echo $q; ?>' ; 

然后

var newVariable = ...   // '222' when $q is 'bbb'
4

1 回答 1

1

享受:

<script>
<?php $q =  (isset($_GET['q'])) ? $_GET['q'] : "default" ;?>

var arr = {
    "aaa": "111",
    "bbb": "222",
    "ccc": "333"
};

var oldVariable = '<?php echo $q ; ?>' ;
var newVariable = (typeof arr['<?php echo $q ; ?>'] != "undefined") ? arr['<?php echo $q ; ?>'] : "No var from get" ;

alert(newVariable) ;

</script>
于 2013-02-19T19:42:13.423 回答