如何使用foreach数组中的PHP创建以下内容?
var options = [
{text: "one", value: 1},
{text: "two", value: 2},
{text: "three", value: 3},
{text: "four", value: 4}
];
Run Code Online (Sandbox Code Playgroud)
我的PHP数组如下所示:
Array
(
[0] => Array
(
[value] => 25000
[text] => 25,000
)
[1] => Array
(
[value] => 25000
[text] => 25,000
)
)
Run Code Online (Sandbox Code Playgroud)
一个简单的JSON编码就可以做到:
echo "<script>var options = " . json_encode($array) . ';</script>';
Run Code Online (Sandbox Code Playgroud)