我需要一个PHP数组的JS数组,特别是格式化,如下所示:
products = [{}, {}, {}]
我已经谷歌2天并试图使用php函数json_encode,但它并没有给我格式我正在寻找。 我得到了这个:
Array
(
[0] => Array
(
[id] => 3971
[name] => Test
[price] => 1107.9000
[brand] => KM
[variant] =>
[quantity] => 3.27
)
[1] => Array
(
[id] => 3957
[name] => Test
[price] => 87.3000
[brand] => KM
[variant] =>
[quantity] => 4
)
)
抱歉愚蠢的重复问题。
答案 0 :(得分:2)
您可以通过php构建您的javascript:
<script>
var foo = <?php echo json_encode(["foo"=>"bar"]); ?>;
</script>
获得
<script>
var foo = {"foo":"bar"};
</script>