2016-08-10 69 views
-5

這是JavaScript代碼,如何將PHP數組數據錶轉換爲javascript?

var tabledata = [ 
 
\t {id:1, name:"Oli Bob", age:"12", gender:"male", height:1, col:"red", dob:"", cheese:1, lucky_no:5}, 
 
\t {id:2, name:"Mary May", age:"1", gender:"female", height:2, col:"blue", dob:"14/05/1982", cheese:true, lucky_no:10}, 
 
\t {id:3, name:"Christine Lobowski", age:"42", height:0, col:"green", dob:"22/05/1982", cheese:"true", lucky_no:12}, 
 
\t {id:4, name:"Brendon Philips", age:"125", gender:"male", height:1, col:"orange", dob:"01/08/1980", lucky_no:18}, 
 
\t {id:5, name:"Margret Marmajuke", age:"16", gender:"female", height:5, col:"yellow", dob:"31/01/1999", lucky_no:33}, 
 
\t {id:6, name:"Frank Harbours", age:"38", gender:"male", height:4, col:"red", dob:"", cheese:1, lucky_no:2}, 
 
\t {id:7, name:"Jamie Newhart", age:"23", gender:"male", height:3, col:"green", dob:"14/05/1985", cheese:true, lucky_no:63}, 
 
\t {id:8, name:"Gemma Jane", age:"60", height:0, col:"red", dob:"22/05/1982", cheese:"true", lucky_no:72}, 
 
\t {id:9, name:"Emily Sykes", age:"42", gender:"female", height:1, col:"maroon", dob:"11/11/1970", lucky_no:44}, 
 
\t {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代碼是像我的第一篇文章一樣的固定代碼。

+0

正確答案是,「這取決於」。 –

+0

我已經改變了他們的伴侶,請檢查他們。謝謝 – Mike

回答

0

我假設你正在尋找json_encode,但你的問題是非常含糊。

編輯: 現在你已經更新了一下。第一,儘量

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

JavaScript需要有某種從PHP輸出的,沒有迴音,什麼可比的,你沒有那個。

+0

這不是一個答案,要麼... O_O –

+0

是的隊友,對不起,我必須具體:( – Mike