2017-02-26 97 views
1

我使用google圖表爲我的Yii2網站繪製圖表。我有PHP數組與下面的格式:將php數組轉換爲具有動態數組大小的js數組

<?php var_dump($a_tg_dat_san_con); ?> 
==> Result 
array(3) { [0]=> array(2) { [0]=> int(1) [1]=> int(0) } [1]=> array(2) { [0]=> int(2) [1]=> int(0) } [2]=> array(2) { [0]=> int(3) [1]=> int(0) } } 

然後,由於建設的dataTable爲谷歌圖的,我上面數組轉換地址爲的JS數組是這樣的:

data_row = [ 
       [<?=json_encode($a_tg_dat_san_con[0][0])?>, <?=json_encode($a_tg_dat_san_con[0][1])?>], 
       [<?=json_encode($a_tg_dat_san_con[1][0])?>, <?=json_encode($a_tg_dat_san_con[1][1])?>], 
       [<?=json_encode($a_tg_dat_san_con[2][0])?>, <?=json_encode($a_tg_dat_san_con[2][1])?>], 
      ]; 

問題是PHP數組是一個動態數組。 我該如何做一個數組轉換的所有情況? 謝謝。

回答

0

整個數組傳遞給javasript變量

var data_row = <?=json_encode($a_tg_dat_san_con);?> 
+0

呀,我雖然太多。 – Sam

0

json_encode支持多維數組。不需要分割所有東西並構建自己的json。

json_encode($a_tg_dat_san_con); 

會給你你正在嘗試構建的東西,它並不關心你有多少行。