如何将json数组传递给java脚本函数并随机访问json数组

时间:2016-03-21 20:43:31

标签: json

我在数据库中有一个名为question的表。表包含四列(q_no,q_id,description,question)。我使用blow代码创建了json数组。

// in php....

$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM question";
$result = $conn->query($sql);
$a=array(); 
while($row = $result->fetch_assoc())
{
     $a[]=$row;
}

$b=json_encode($a);  
myfunction($b)
{
}

Java脚本功能是:

// inside script
function myfuction(x)
{
   // how to access x[2].description
}

现在的问题是如何将$ b传递给java脚本函数以及如何访问特定行的特定列的值?????

1 个答案:

答案 0 :(得分:0)

这样的事情:

echo "<script> a = $b; alert(a[0].description); </script>";