如何将PHP数组数据表转换为javascript?

时间:2016-08-10 19:43:16

标签: javascript arrays ivalueconverter

这是JavaScript代码,

var tabledata = [
	{id:1, name:"Oli Bob", age:"12", gender:"male", height:1, col:"red", dob:"", cheese:1, lucky_no:5},
	{id:2, name:"Mary May", age:"1", gender:"female", height:2, col:"blue", dob:"14/05/1982", cheese:true, lucky_no:10},
	{id:3, name:"Christine Lobowski", age:"42", height:0, col:"green", dob:"22/05/1982", cheese:"true", lucky_no:12},
	{id:4, name:"Brendon Philips", age:"125", gender:"male", height:1, col:"orange", dob:"01/08/1980", lucky_no:18},
	{id:5, name:"Margret Marmajuke", age:"16", gender:"female", height:5, col:"yellow", dob:"31/01/1999", lucky_no:33},
	{id:6, name:"Frank Harbours", age:"38", gender:"male", height:4, col:"red", dob:"", cheese:1, lucky_no:2},
	{id:7, name:"Jamie Newhart", age:"23", gender:"male", height:3, col:"green", dob:"14/05/1985", cheese:true, lucky_no:63},
	{id:8, name:"Gemma Jane", age:"60", height:0, col:"red", dob:"22/05/1982", cheese:"true", lucky_no:72},
	{id:9, name:"Emily Sykes", age:"42", gender:"female", height:1, col:"maroon", dob:"11/11/1970", lucky_no:44},
	{id:10, name:"James Newman", age:"73", gender:"male", height:5, col:"red", dob:"22/03/1998", lucky_no:9},
];
this is my php code :

<?php $arr1 = array (
array('id'=>'1', 'name'=>'Oli Bob','age'=>'12', 'gender'=>'male','height'=>'1','col'=>'red','dob'=>'', 'cheese'=>'1','lucky_no'=>'5'),
array('id'=>'2', 'name'=>'Mary May','age'=>'1', 'gender'=>'female','height'=>'2','col'=>'blue','dob'=>'14/05/1982', 'cheese'=>'true','lucky_no'=>'10')
);?>

and javascript :

var tabledata = "<?php json_encode($arr1);?>";

Please Help...? :(

我的qustion是,在PHP代码中javascript可能在我的尝试中错了..! javascript代码是我第一篇文章的固定代码。

1 个答案:

答案 0 :(得分:-1)

我假设您正在寻找json_encode,但您的问题非常模糊。

编辑: 现在你已经更新了一下。首先,试试

var tabledata = "<?php echo json_encode($arr1); ?>";

javascript需要从php获得某种输出,没有回声,或类似的东西,你就没有。