对于PHP数组转换为JavaScript的最好的方法 [英] Best method for converting a PHP array to javascript

查看:80
本文介绍了对于PHP数组转换为JavaScript的最好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递一个PHP数组到一个JavaScript函数。我知道要做到这一点的唯一方法是创建一个从PHP数组一个JS数组并传递给js函数。但是这创造了code的大块转移(见下文code - 有很多更多,但我敢肯定,你得到的总体思路)。我怀疑有一个更有效的方法。会有人关心与我分享呢?谢谢!

 <脚本类型=文/ JavaScript的>
    jsInit(http://railsacrosseurope.com/public
           http://railsacrosseurope.com
           85572859349);无功routeCount = [75];
    VAR火车= [75];
      培养[1] = [];
      培养[1] [0] = [];
      列车[1] [0] [PLAYER_NUM'] = 5;
      火车[1] [0] ['行'] ='红';
      列车[1] [0] [track_unit'] = 0;
      培养[1] [0] ['方向'] ='_';
      培养[1] [0] [dest_city'] = 43;
      火车[1] [0] ['状态'] ='ARRIVED';
      列车[1] [0] [ROUTE_ID'] = 1;
      培养[1] [0] [产品'] = [];
      routeCount [1] = [];
      routeCount [1] ['ROUTE_ID'] = 1;
      routeCount [1] ['伯爵'] = drawTrack(68,658,149,568,888#,火车[1]);
      培养[2] = [];
      routeCount [2] = [];
      routeCount [2] ['ROUTE_ID'] = 2;
      routeCount [2] ['计数'] = drawTrack(10,571,149,568,'#888',火车[2]);
      培养[3] = [];
      routeCount [3] = [];
      routeCount [3] ['ROUTE_ID'] = 3;
      routeCount [3] ['计数'] = drawTrack(53,510,149,568,'#888',火车[3]);
      培养[4] = [];
      routeCount [4] = [];
      routeCount [4] ['ROUTE_ID'] = 4;
      routeCount [4] ['计数'] = drawTrack(149,568,202,495,'#888',火车[4]);
      培养[5] = [];
      培养[5] [0] = [];
      列车[5] [0] [PLAYER_NUM'] = 4;
      列车[5] [0] ['行'] ='红';
      列车[5] [0] [track_unit'] = 2;
      培养[5] [0] ['方向'] ='_';
      培养[5] [0] [dest_city'] = 56;
      列车[5] [0] ['状态'] ='航路';
      列车[5] [0] [ROUTE_ID'] = 5;
      培养[5] [0] [产品'] = [];
      routeCount [5] = [];
      routeCount [5] ['ROUTE_ID'] = 5;
      routeCount [5] ['计数'] = drawTrack(149,568,217,605,'#888',火车[5]);
      培养[6] = [];
      routeCount [6] = [];
      routeCount [6] ['ROUTE_ID'] = 6;
      routeCount [6] ['计数'] = drawTrack(217,605,282,572,'#888',火车[6]);
      培养[7] = [];
      routeCount [7] = [];
      routeCount [7] ['ROUTE_ID'] = 7;
      routeCount [7] ['计数'] = drawTrack(202,495,250,448,'#888',火车[7]);
      培养[8] = [];
      routeCount [8] = [];
      routeCount [8] ['ROUTE_ID'] = 8;
      routeCount [8] ['计数'] = drawTrack(282,572,293,504,'#888',火车[8]);
      培养[10] = [];
      routeCount [9] = [];
      routeCount [9] ['ROUTE_ID'] = 9;
      routeCount [9] ['计数'] = drawTrack(293,504,250,448,'#888',火车[9]);
      列车[10] = [];
      routeCount [10] = [];
      routeCount [10] ['ROUTE_ID'] = 10;
      routeCount [10] ['计数'] = drawTrack(250,448,370,450,'#888',火车[10]);
      火车[11] = [];
      routeCount [11] = [];
      routeCount [11] ['ROUTE_ID'] = 11;
      routeCount [11] ['计数'] = drawTrack(293,504,373,530,'#888',火车[11]);
      火车[12] = [];
      routeCount [12] = [];
      routeCount [12] ['ROUTE_ID'] = 12;
      routeCount [12] ['计数'] = drawTrack(250,448,250,372,'#888',火车[12]);
      火车[13] = [];
      routeCount [13] = [];
      routeCount [13] ['ROUTE_ID'] = 13;
      routeCount [13] ['计数'] = drawTrack(373,530,431,479,'#888',火车[13]);
      列车[14] = [];
      routeCount [14] = [];
      routeCount [14] ['ROUTE_ID'] = 14;
      routeCount [14] ['伯爵'] = drawTrack(373,530,370,450,888#,火车[14]);
      列车[15] = [];
      routeCount [15] = [];
      routeCount [15] ['ROUTE_ID'] = 15;
      routeCount [15] ['计数'] = drawTrack(431,479,466,479,'#888',火车[15]);
      火车[16] = [];
      routeCount [16] = [];
      routeCount [16] ['ROUTE_ID'] = 16;
      routeCount [16] ['计数'] = drawTrack(466,479,527,484,'#888',火车[16]);
      列车[17] = [];
      routeCount [17] = [];
      routeCount [17] ['ROUTE_ID'] = 17;
      routeCount [17] ['计数'] = drawTrack(527,484,511,521,'#888',火车[17]);
      列车[18] = [];
      列车[18] [0] = [];
      列车[18] [0] [PLAYER_NUM'] = 2;
      火车[18] [0] ['行'] ='红';
      列车[18] [0] [track_unit'] = 3;
      培养[18] [0] ['方向'] ='_';
      培养[18] [0] [dest_city'] = 17;
      火车[18] [0] ['状态'] ='航路';
      火车[18] [0] ['ROUTE_ID'] = 18;
      培养[18] [0] [产品'] = [];
      列车[18] [0] [货物] [0] = [];
      火车[18] [0] ['品'] [0] ['名'] ='家具';
      火车[18] [0] ['品'] [0] ['goods_id'] = 31;
      列车[18] [0] [货物] [1] = [];
      火车[18] [0] ['品'] [1] ['名'] ='巧克力';
      火车[18] [0] ['品'] [1] ['goods_id'] = 7;
      routeCount [18] = [];
      routeCount [18] ['ROUTE_ID'] = 18;
      routeCount [18] ['伯爵'] = drawTrack(511,521,527,598,888#,火车[18]);
      火车[19] = [];
      routeCount [19] = [];
      routeCount [19] ['ROUTE_ID'] = 19;
      routeCount [19] ['计数'] = drawTrack(527,598,584,650,'#888',火车[19]);
      火车[20] = [];
      routeCount [20] = [];
      routeCount [20] ['ROUTE_ID'] = 20;
      routeCount [20] ['计数'] = drawTrack(466,479,432,430,'#888',火车[20]);
      火车[21] = [];
      routeCount [21] = [];
      routeCount [21] ['ROUTE_ID'] = 21;
      routeCount [21] ['伯爵'] = drawTrack(466,479,462,430,888#,火车[21]);
      火车[22] = [];
      routeCount [22] = [];
      routeCount [22] ['ROUTE_ID'] = 22;
      routeCount [22] ['伯爵'] = drawTrack(370,450,344,350,888#,火车[22]);
      火车[23] = [];
      routeCount [23] = [];
      routeCount [23] ['ROUTE_ID'] = 23;
      routeCount [23] ['伯爵'] = drawTrack(250,372,344,350,888#,火车[23]);
      火车[24] = [];
      routeCount [24] = [];
      routeCount [24] ['ROUTE_ID'] = 24;
      routeCount [24] ['伯爵'] = drawTrack(344,350,314,260,888#,火车[24]);
      火车[25] = [];
      routeCount [25] = [];
      routeCount [25] ['ROUTE_ID'] = 25;
      routeCount [25] ['伯爵'] = drawTrack(344,350,395,299,888#,火车[25]);
      火车[26] = [];
      routeCount [26] = [];
      routeCount [26] ['ROUTE_ID'] = 26;
      routeCount [26] ['伯爵'] = drawTrack(395,299,400,290,888#,火车[26]);
      火车[27] ​​= [];
      routeCount [27] ​​= [];
      routeCount [27] ​​['ROUTE_ID'] = 27;
      routeCount [27] ​​['计数'] = drawTrack(400,290,412,255,'#888',火车[27]);
      列车[28] = [];
      routeCount [28] = [];
      routeCount [28] ['ROUTE_ID'] = 28;
      routeCount [28] ['伯爵'] = drawTrack(412,255,314,260,888#,火车[28]);
      火车[29] = [];
      routeCount [29] = [];
      routeCount [29] ['ROUTE_ID'] = 29;
      routeCount [29] ['伯爵'] = drawTrack(314,260,257,232,888#,火车[29]);
      火车[30] = [];
      列车[30] [0] = [];
      列车[30] [0] [PLAYER_NUM'] = 3;
      火车[30] [0] ['行'] ='红';
      列车[30] [0] [track_unit'] = 2;
      培养[30] [0] ['方向'] ='_';
      培养[30] [0] [dest_city'] = 29;
      火车[30] [0] ['状态'] ='航路';
      列车[30] [0] [ROUTE_ID'] = 30;
      训练[30] [0] ['货'] = [];
      火车[30] [0] ['品'] [0] = [];
      火车[30] [0] ['品'] [0] ['名'] ='大理石';
      火车[30] [0] ['品'] [0] ['goods_id'] = 19;
      routeCount [30] = [];
      routeCount [30] ['ROUTE_ID'] = 30;
      routeCount [30] ['伯爵'] = drawTrack(314,260,300,209,888#,火车[30]);
      火车[31] = [];
      routeCount [31] = [];
      routeCount [31] ['ROUTE_ID'] = 31;
      routeCount [31] ['伯爵'] = drawTrack(300,209,308,192,888#,火车[31]);
      火车[32] = [];
      routeCount [32] = [];
      routeCount [32] ['ROUTE_ID'] = 32;
      routeCount [32] ['伯爵'] = drawTrack(308,192,225,170,888#,火车[32]);
      列车[33] = [];
      routeCount [33] = [];
      routeCount [33] ['ROUTE_ID'] = 33;
      routeCount [33] ['伯爵'] = drawTrack(225,170,166,195,888#,火车[33]);
      火车[34] = [];
      routeCount [34] = [];
      routeCount [34] ['ROUTE_ID'] = 34;
      routeCount [34] ['伯爵'] = drawTrack(308,192,315,155,'555号',火车[34]);
      火车[35] = [];
      routeCount [35] = [];
      routeCount [35] ['ROUTE_ID'] = 35;
      routeCount [35] ['伯爵'] = drawTrack(315,155,285,101,888#,火车[35]);
      火车[36] = [];
      routeCount [36] = [];
      routeCount [36] ['ROUTE_ID'] = 36;
      routeCount [36] ['伯爵'] = drawTrack(285,101,330,70,'#888',列车[36]);
      火车[37] = [];
      routeCount [37] = [];
      routeCount [37] ['ROUTE_ID'] = 37;
      routeCount [37] ['伯爵'] = drawTrack(285,101,243,131,888#,火车[37]);
      火车[38] = [];
      routeCount [38] = [];
      routeCount [38] ['ROUTE_ID'] = 38;
      routeCount [38] ['伯爵'] = drawTrack(412,255,461,304,'555号',火车[38]);
      火车[39] = [];
      routeCount [39] = [];
      routeCount [39] ['ROUTE_ID'] = 39;
      routeCount [39] ['伯爵'] = drawTrack(461,304,499,350,'555号',火车[39]);
      火车[40] = [];
      routeCount [40] = [];
      routeCount [40] ['ROUTE_ID'] = 40;
      routeCount [40] ['伯爵'] = drawTrack(499,350,504,370,888#,火车[40]);
      火车[41] = [];
      routeCount [41] = [];
      routeCount [41] ['ROUTE_ID'] = 41;
      routeCount [41] ['伯爵'] = drawTrack(504,370,462,430,888#,火车[41]);
      火车[42] = [];
      routeCount [42] = [];
      routeCount [42] ['ROUTE_ID'] = 42;
      routeCount [42] ['伯爵'] = drawTrack(462,430,532,400,888#,火车[42]);
      火车[43] = [];
      routeCount [43] = [];
      routeCount [43] ['ROUTE_ID'] = 43;
      routeCount [43] ['伯爵'] = drawTrack(532,400,623,408,888#,火车[43]);
      火车[44] = [];
      routeCount [44] = [];
      routeCount [44] ['ROUTE_ID'] = 44;
      routeCount [44] ['伯爵'] = drawTrack(461,304,554,300,'555号',火车[44]);
      火车[45] = [];
      routeCount [45] = [];
      routeCount [45] ['ROUTE_ID'] = 45;
      routeCount [45] ['伯爵'] = drawTrack(554,300,573,270,888#,火车[45]);
      火车[46] = [];
      routeCount [46] = [];
      routeCount [46] ['ROUTE_ID'] = 46;
      routeCount [46] ['伯爵'] = drawTrack(554,300,585,350,888#,火车[46]);
      火车[47] = [];
      routeCount [47] = [];
      routeCount [47] ['ROUTE_ID'] = 47;
      routeCount [47] ['伯爵'] = drawTrack(585,350,623,408,888#,火车[47]);
      火车[48] = [];
      routeCount [48] = [];
      routeCount [48] ['ROUTE_ID'] = 48;
      routeCount [48] ['伯爵'] = drawTrack(527,484,610,470,888#,火车[48]);
      火车[49] = [];
      routeCount [49] = [];
      routeCount [49] ['ROUTE_ID'] = 49;
      routeCount [49] ['伯爵'] = drawTrack(610,470,623,408,888#,火车[49]);
      火车[50] = [];
      routeCount [50] = [];
      routeCount [50] ['ROUTE_ID'] = 50;
      routeCount [50] ['伯爵'] = drawTrack(610,470,659,540,888#,火车[50]);
      火车[51] = [];
      routeCount [51] = [];
      routeCount [51] ['ROUTE_ID'] = 51;
      routeCount [51] ['伯爵'] = drawTrack(659,540,705,510,888#,火车[51]);
      火车[52​​] = [];
      routeCount [52] = [];
      routeCount [52] ['ROUTE_ID'] = 52;
      routeCount [52] ['伯爵'] = drawTrack(705,510,610,470,'555号',火车[52​​]);
      火车[53] = [];
      routeCount [53] = [];
      routeCount [53] ['ROUTE_ID'] = 53;
      routeCount [53] ['伯爵'] = drawTrack(705,510,673,433,888#,火车[53]);
      火车[54] = [];
      routeCount [54] = [];
      routeCount [54] ['ROUTE_ID'] = 54;
      routeCount [54] ['伯爵'] = drawTrack(610,470,673,433,888#,火车[54]);
      火车[55] = [];
      routeCount [55] = [];
      routeCount [55] ['ROUTE_ID'] = 55;
      routeCount [55] ['伯爵'] = drawTrack(673,433,623,408,888#,火车[55]);
      火车[56] = [];
      routeCount [56] = [];
      routeCount [56] ['ROUTE_ID'] = 56;
      routeCount [56] ['伯爵'] = drawTrack(623,408,693,338,888#,火车[56]);
      火车[57] = [];
      routeCount [57] = [];
      routeCount [57] ['ROUTE_ID'] = 57;
      routeCount [57] ['伯爵'] = drawTrack(693,338,675,298,888#,火车[57]);
      火车[58] = [];
      routeCount [58] = [];
      routeCount [58] ['ROUTE_ID'] = 58;
      routeCount [58] ['伯爵'] = drawTrack(675,298,710,282,888#,火车[58]);
      火车[59] = [];
      routeCount [59] = [];
      routeCount [59] ['ROUTE_ID'] = 59;
      routeCount [59] ['伯爵'] = drawTrack(710,282,699,210,888#,火车[59]);
      火车[60] = [];
      routeCount [60] = [];
      routeCount [60] ['ROUTE_ID'] = 60;
      routeCount [60] ['伯爵'] = drawTrack(585,350,639,314,888#,火车[60]);
      火车[61] = [];
      routeCount [61] = [];
      routeCount [61] ['ROUTE_ID'] = 61;
      routeCount [61] ['伯爵'] = drawTrack(639,314,675,298,888#,火车[61]);
      火车[62] = [];
      routeCount [62] = [];
      routeCount [62] ['ROUTE_ID'] = 62;
      routeCount [62] ['伯爵'] = drawTrack(461,304,490,232,'555号',火车[62]);
      火车[63] = [];
      routeCount [63] = [];
      routeCount [63] ['ROUTE_ID'] = 63;
      routeCount [63] ['伯爵'] = drawTrack(490,232,513,230,'555号',火车[63]);
      火车[64] = [];
      routeCount [64] = [];
      routeCount [64] ['ROUTE_ID'] = 64;
      routeCount [64] ['伯爵'] = drawTrack(573,270,513,230,'555号',火车[64]);
      火车[65] = [];
      routeCount [65] = [];
      routeCount [65] ['ROUTE_ID'] = 65;
      routeCount [65] ['伯爵'] = drawTrack(573,270,596,240,888#,火车[65]);
      火车[66] = [];
      routeCount [66] = [];
      routeCount [66] ['ROUTE_ID'] = 66;
      routeCount [66] ['伯爵'] = drawTrack(596,240,560,170,888#,火车[66]);
      火车[67] = [];
      列车[67] [0] = [];
      列车[67] [0] [PLAYER_NUM'] = 1;
      火车[67] [0] ['行'] ='红';
      列车[67] [0] [track_unit'] = 8;
      列车[67] [0] ['方向'] ='+';
      列车[67] [0] [dest_city'] = 3;
      火车[67] [0] ['状态'] ='ARRIVED';
      列车[67] [0] [ROUTE_ID'] = 67;
      训练[67] [0] ['货'] = [];
      火车[67] [0] ['品'] [0] = [];
      火车[67] [0] ['品'] [0] ['名'] ='木材';
      火车[67] [0] ['品'] [0] ['goods_id'] = 29;
      routeCount [67] = [];
      routeCount [67] ['ROUTE_ID'] = 67;
      routeCount [67] ['伯爵'] = drawTrack(513,230,528,150,'555号',火车[67]);
      火车[68] = [];
      routeCount [68] = [];
      routeCount [68] ['ROUTE_ID'] = 68;
      routeCount [68] ['伯爵'] = drawTrack(528,150,560,170,888#,火车[68]);
      火车[69] = [];
      routeCount [69] = [];
      routeCount [69] ['ROUTE_ID'] = 69;
      routeCount [69] ['伯爵'] = drawTrack(560,170,567,112,888#,火车[69]);
      火车[70] = [];
      routeCount [70] = [];
      routeCount [70] ['ROUTE_ID'] = 70;
      routeCount [70] ['伯爵'] = drawTrack(567,112,553,38,'#555',列车[70]);
      火车[71] = [];
      routeCount [71] = [];
      routeCount [71] ['ROUTE_ID'] = 71;
      routeCount [71] ['伯爵'] = drawTrack(560,170,661,61,'#888',列车[71]);
      火车[72] = [];
      routeCount [72] = [];
      routeCount [72] ['ROUTE_ID'] = 72;
      routeCount [72] ['伯爵'] = drawTrack(553,38,661,61,'#555',列车[72]);
      火车[73] = [];
      routeCount [73] = [];
      routeCount [73] ['ROUTE_ID'] = 73;
      routeCount [73] ['伯爵'] = drawTrack(344,350,425,340,888#,火车[73]);
      火车[74] = [];
      routeCount [74] = [];
      routeCount [74] ['ROUTE_ID'] = 74;
      routeCount [74] ['伯爵'] = drawTrack(425,340,461,304,888#,火车[74]);
      火车[75] = [];
      routeCount [75] = [];
      routeCount [75] ['ROUTE_ID'] = 75;
      routeCount [75] ['伯爵'] = drawTrack(425,340,499,350,'555号',火车[75]);
    < / SCRIPT>


解决方案

使用JSON。为什么JSON在XML:<一href=\"http://thephp$c$c.blogspot.com/2008/08/why-json-over-xml-in-ajax.html\">http://thephp$c$c.blogspot.com/2008/08/why-json-over-xml-in-ajax.html

要在PHP中使用JSON简单:

 &LT; PHP  呼应'&LT;脚本类型=文/ JavaScript的&GT; / *&LT;![CDATA [* /';
  回声无功火车='.json_en code($数组);
  回声/ *]]&GT; * /&LT; / SCRIPT&GT;';?&GT;

变量列车在Javascript将包含基于PHP的变量 $阵列属性和数组的对象。

要分析或通过火车对象,你可以使用为中... 报表JSON阵列,并直接使用object.property为JSON对象。

显示方式:

 &LT; PHP  $阵列= array(array('id'=>3,'title'=>'Great2'),array('id'=>5,'title'=>'Great'),array('id'=>1,'title'=>'Great3'))
  呼应'&LT;脚本类型=文/ JavaScript的&GT; / *&LT;![CDATA [* /';
  回声无功火车='.json_en code($数组);
  回声/ *]]&GT; * /&LT; / SCRIPT&GT;';?&GT;

的输出将是:

  VAR火车= {[ID:3,标题:Great2'},{ID:5,标题:大},{ID:1,标题:Great3 }];

变量列车成为对象的数组。 [] squrare括号是数组,举行更多的数组或对象。 {}花括号里的对象,他们的属性给他们。

所以要遍历列车变量:

 &LT;脚本类型=文/ JavaScript的&GT;  VAR火车= {[ID:3,标题:Great2'},{ID:5,标题:大},{ID:1,标题:Great3'}]  为(i的列车){
    VAR T =火车[I] //获取数组的当前元素
    警报(t.id); //获取ID
    警报(t.title); //获取标题
  }&LT; / SCRIPT&GT;

简单!希望确实有帮助。

I am passing a PHP array to a javascript function. The only way I know to do this is to create a js array from the PHP array and pass that to the js function. But this creates a huge chunk of code to transfer (see code below - there's a lot more but I'm sure you get the general idea). I suspect that there is a more efficient method. Would anyone care to share it with me? Thanks!

   <script type="text/javascript">
    jsInit("http://railsacrosseurope.com/public",
           "http://railsacrosseurope.com",
           "85572859349");var routeCount = [75];
    var train = [75];
      train[1] = [];
      train[1][0] = [];
      train[1][0]['player_num'] = 5;
      train[1][0]['line'] = 'RED';
      train[1][0]['track_unit'] = 0;
      train[1][0]['direction'] = '_';
      train[1][0]['dest_city'] = 43;
      train[1][0]['status'] = 'ARRIVED';
      train[1][0]['route_id'] = 1;
      train[1][0]['goods'] = [];
      routeCount[1] = [];
      routeCount[1]['route_id'] = 1;
      routeCount[1]['count'] = drawTrack(68, 658, 149, 568, '#888', train[1]);
      train[2] = [];
      routeCount[2] = [];
      routeCount[2]['route_id'] = 2;
      routeCount[2]['count'] = drawTrack(10, 571, 149, 568, '#888', train[2]);
      train[3] = [];
      routeCount[3] = [];
      routeCount[3]['route_id'] = 3;
      routeCount[3]['count'] = drawTrack(53, 510, 149, 568, '#888', train[3]);
      train[4] = [];
      routeCount[4] = [];
      routeCount[4]['route_id'] = 4;
      routeCount[4]['count'] = drawTrack(149, 568, 202, 495, '#888', train[4]);
      train[5] = [];
      train[5][0] = [];
      train[5][0]['player_num'] = 4;
      train[5][0]['line'] = 'RED';
      train[5][0]['track_unit'] = 2;
      train[5][0]['direction'] = '_';
      train[5][0]['dest_city'] = 56;
      train[5][0]['status'] = 'ENROUTE';
      train[5][0]['route_id'] = 5;
      train[5][0]['goods'] = [];
      routeCount[5] = [];
      routeCount[5]['route_id'] = 5;
      routeCount[5]['count'] = drawTrack(149, 568, 217, 605, '#888', train[5]);
      train[6] = [];
      routeCount[6] = [];
      routeCount[6]['route_id'] = 6;
      routeCount[6]['count'] = drawTrack(217, 605, 282, 572, '#888', train[6]);
      train[7] = [];
      routeCount[7] = [];
      routeCount[7]['route_id'] = 7;
      routeCount[7]['count'] = drawTrack(202, 495, 250, 448, '#888', train[7]);
      train[8] = [];
      routeCount[8] = [];
      routeCount[8]['route_id'] = 8;
      routeCount[8]['count'] = drawTrack(282, 572, 293, 504, '#888', train[8]);
      train[9] = [];
      routeCount[9] = [];
      routeCount[9]['route_id'] = 9;
      routeCount[9]['count'] = drawTrack(293, 504, 250, 448, '#888', train[9]);
      train[10] = [];
      routeCount[10] = [];
      routeCount[10]['route_id'] = 10;
      routeCount[10]['count'] = drawTrack(250, 448, 370, 450, '#888', train[10]);
      train[11] = [];
      routeCount[11] = [];
      routeCount[11]['route_id'] = 11;
      routeCount[11]['count'] = drawTrack(293, 504, 373, 530, '#888', train[11]);
      train[12] = [];
      routeCount[12] = [];
      routeCount[12]['route_id'] = 12;
      routeCount[12]['count'] = drawTrack(250, 448, 250, 372, '#888', train[12]);
      train[13] = [];
      routeCount[13] = [];
      routeCount[13]['route_id'] = 13;
      routeCount[13]['count'] = drawTrack(373, 530, 431, 479, '#888', train[13]);
      train[14] = [];
      routeCount[14] = [];
      routeCount[14]['route_id'] = 14;
      routeCount[14]['count'] = drawTrack(373, 530, 370, 450, '#888', train[14]);
      train[15] = [];
      routeCount[15] = [];
      routeCount[15]['route_id'] = 15;
      routeCount[15]['count'] = drawTrack(431, 479, 466, 479, '#888', train[15]);
      train[16] = [];
      routeCount[16] = [];
      routeCount[16]['route_id'] = 16;
      routeCount[16]['count'] = drawTrack(466, 479, 527, 484, '#888', train[16]);
      train[17] = [];
      routeCount[17] = [];
      routeCount[17]['route_id'] = 17;
      routeCount[17]['count'] = drawTrack(527, 484, 511, 521, '#888', train[17]);
      train[18] = [];
      train[18][0] = [];
      train[18][0]['player_num'] = 2;
      train[18][0]['line'] = 'RED';
      train[18][0]['track_unit'] = 3;
      train[18][0]['direction'] = '_';
      train[18][0]['dest_city'] = 17;
      train[18][0]['status'] = 'ENROUTE';
      train[18][0]['route_id'] = 18;
      train[18][0]['goods'] = [];
      train[18][0]['goods'][0] = [];
      train[18][0]['goods'][0]['name'] = 'Furniture';
      train[18][0]['goods'][0]['goods_id'] = 31;
      train[18][0]['goods'][1] = [];
      train[18][0]['goods'][1]['name'] = 'Chocolate';
      train[18][0]['goods'][1]['goods_id'] = 7;
      routeCount[18] = [];
      routeCount[18]['route_id'] = 18;
      routeCount[18]['count'] = drawTrack(511, 521, 527, 598, '#888', train[18]);
      train[19] = [];
      routeCount[19] = [];
      routeCount[19]['route_id'] = 19;
      routeCount[19]['count'] = drawTrack(527, 598, 584, 650, '#888', train[19]);
      train[20] = [];
      routeCount[20] = [];
      routeCount[20]['route_id'] = 20;
      routeCount[20]['count'] = drawTrack(466, 479, 432, 430, '#888', train[20]);
      train[21] = [];
      routeCount[21] = [];
      routeCount[21]['route_id'] = 21;
      routeCount[21]['count'] = drawTrack(466, 479, 462, 430, '#888', train[21]);
      train[22] = [];
      routeCount[22] = [];
      routeCount[22]['route_id'] = 22;
      routeCount[22]['count'] = drawTrack(370, 450, 344, 350, '#888', train[22]);
      train[23] = [];
      routeCount[23] = [];
      routeCount[23]['route_id'] = 23;
      routeCount[23]['count'] = drawTrack(250, 372, 344, 350, '#888', train[23]);
      train[24] = [];
      routeCount[24] = [];
      routeCount[24]['route_id'] = 24;
      routeCount[24]['count'] = drawTrack(344, 350, 314, 260, '#888', train[24]);
      train[25] = [];
      routeCount[25] = [];
      routeCount[25]['route_id'] = 25;
      routeCount[25]['count'] = drawTrack(344, 350, 395, 299, '#888', train[25]);
      train[26] = [];
      routeCount[26] = [];
      routeCount[26]['route_id'] = 26;
      routeCount[26]['count'] = drawTrack(395, 299, 400, 290, '#888', train[26]);
      train[27] = [];
      routeCount[27] = [];
      routeCount[27]['route_id'] = 27;
      routeCount[27]['count'] = drawTrack(400, 290, 412, 255, '#888', train[27]);
      train[28] = [];
      routeCount[28] = [];
      routeCount[28]['route_id'] = 28;
      routeCount[28]['count'] = drawTrack(412, 255, 314, 260, '#888', train[28]);
      train[29] = [];
      routeCount[29] = [];
      routeCount[29]['route_id'] = 29;
      routeCount[29]['count'] = drawTrack(314, 260, 257, 232, '#888', train[29]);
      train[30] = [];
      train[30][0] = [];
      train[30][0]['player_num'] = 3;
      train[30][0]['line'] = 'RED';
      train[30][0]['track_unit'] = 2;
      train[30][0]['direction'] = '_';
      train[30][0]['dest_city'] = 29;
      train[30][0]['status'] = 'ENROUTE';
      train[30][0]['route_id'] = 30;
      train[30][0]['goods'] = [];
      train[30][0]['goods'][0] = [];
      train[30][0]['goods'][0]['name'] = 'Marble';
      train[30][0]['goods'][0]['goods_id'] = 19;
      routeCount[30] = [];
      routeCount[30]['route_id'] = 30;
      routeCount[30]['count'] = drawTrack(314, 260, 300, 209, '#888', train[30]);
      train[31] = [];
      routeCount[31] = [];
      routeCount[31]['route_id'] = 31;
      routeCount[31]['count'] = drawTrack(300, 209, 308, 192, '#888', train[31]);
      train[32] = [];
      routeCount[32] = [];
      routeCount[32]['route_id'] = 32;
      routeCount[32]['count'] = drawTrack(308, 192, 225, 170, '#888', train[32]);
      train[33] = [];
      routeCount[33] = [];
      routeCount[33]['route_id'] = 33;
      routeCount[33]['count'] = drawTrack(225, 170, 166, 195, '#888', train[33]);
      train[34] = [];
      routeCount[34] = [];
      routeCount[34]['route_id'] = 34;
      routeCount[34]['count'] = drawTrack(308, 192, 315, 155, '#555', train[34]);
      train[35] = [];
      routeCount[35] = [];
      routeCount[35]['route_id'] = 35;
      routeCount[35]['count'] = drawTrack(315, 155, 285, 101, '#888', train[35]);
      train[36] = [];
      routeCount[36] = [];
      routeCount[36]['route_id'] = 36;
      routeCount[36]['count'] = drawTrack(285, 101, 330, 70, '#888', train[36]);
      train[37] = [];
      routeCount[37] = [];
      routeCount[37]['route_id'] = 37;
      routeCount[37]['count'] = drawTrack(285, 101, 243, 131, '#888', train[37]);
      train[38] = [];
      routeCount[38] = [];
      routeCount[38]['route_id'] = 38;
      routeCount[38]['count'] = drawTrack(412, 255, 461, 304, '#555', train[38]);
      train[39] = [];
      routeCount[39] = [];
      routeCount[39]['route_id'] = 39;
      routeCount[39]['count'] = drawTrack(461, 304, 499, 350, '#555', train[39]);
      train[40] = [];
      routeCount[40] = [];
      routeCount[40]['route_id'] = 40;
      routeCount[40]['count'] = drawTrack(499, 350, 504, 370, '#888', train[40]);
      train[41] = [];
      routeCount[41] = [];
      routeCount[41]['route_id'] = 41;
      routeCount[41]['count'] = drawTrack(504, 370, 462, 430, '#888', train[41]);
      train[42] = [];
      routeCount[42] = [];
      routeCount[42]['route_id'] = 42;
      routeCount[42]['count'] = drawTrack(462, 430, 532, 400, '#888', train[42]);
      train[43] = [];
      routeCount[43] = [];
      routeCount[43]['route_id'] = 43;
      routeCount[43]['count'] = drawTrack(532, 400, 623, 408, '#888', train[43]);
      train[44] = [];
      routeCount[44] = [];
      routeCount[44]['route_id'] = 44;
      routeCount[44]['count'] = drawTrack(461, 304, 554, 300, '#555', train[44]);
      train[45] = [];
      routeCount[45] = [];
      routeCount[45]['route_id'] = 45;
      routeCount[45]['count'] = drawTrack(554, 300, 573, 270, '#888', train[45]);
      train[46] = [];
      routeCount[46] = [];
      routeCount[46]['route_id'] = 46;
      routeCount[46]['count'] = drawTrack(554, 300, 585, 350, '#888', train[46]);
      train[47] = [];
      routeCount[47] = [];
      routeCount[47]['route_id'] = 47;
      routeCount[47]['count'] = drawTrack(585, 350, 623, 408, '#888', train[47]);
      train[48] = [];
      routeCount[48] = [];
      routeCount[48]['route_id'] = 48;
      routeCount[48]['count'] = drawTrack(527, 484, 610, 470, '#888', train[48]);
      train[49] = [];
      routeCount[49] = [];
      routeCount[49]['route_id'] = 49;
      routeCount[49]['count'] = drawTrack(610, 470, 623, 408, '#888', train[49]);
      train[50] = [];
      routeCount[50] = [];
      routeCount[50]['route_id'] = 50;
      routeCount[50]['count'] = drawTrack(610, 470, 659, 540, '#888', train[50]);
      train[51] = [];
      routeCount[51] = [];
      routeCount[51]['route_id'] = 51;
      routeCount[51]['count'] = drawTrack(659, 540, 705, 510, '#888', train[51]);
      train[52] = [];
      routeCount[52] = [];
      routeCount[52]['route_id'] = 52;
      routeCount[52]['count'] = drawTrack(705, 510, 610, 470, '#555', train[52]);
      train[53] = [];
      routeCount[53] = [];
      routeCount[53]['route_id'] = 53;
      routeCount[53]['count'] = drawTrack(705, 510, 673, 433, '#888', train[53]);
      train[54] = [];
      routeCount[54] = [];
      routeCount[54]['route_id'] = 54;
      routeCount[54]['count'] = drawTrack(610, 470, 673, 433, '#888', train[54]);
      train[55] = [];
      routeCount[55] = [];
      routeCount[55]['route_id'] = 55;
      routeCount[55]['count'] = drawTrack(673, 433, 623, 408, '#888', train[55]);
      train[56] = [];
      routeCount[56] = [];
      routeCount[56]['route_id'] = 56;
      routeCount[56]['count'] = drawTrack(623, 408, 693, 338, '#888', train[56]);
      train[57] = [];
      routeCount[57] = [];
      routeCount[57]['route_id'] = 57;
      routeCount[57]['count'] = drawTrack(693, 338, 675, 298, '#888', train[57]);
      train[58] = [];
      routeCount[58] = [];
      routeCount[58]['route_id'] = 58;
      routeCount[58]['count'] = drawTrack(675, 298, 710, 282, '#888', train[58]);
      train[59] = [];
      routeCount[59] = [];
      routeCount[59]['route_id'] = 59;
      routeCount[59]['count'] = drawTrack(710, 282, 699, 210, '#888', train[59]);
      train[60] = [];
      routeCount[60] = [];
      routeCount[60]['route_id'] = 60;
      routeCount[60]['count'] = drawTrack(585, 350, 639, 314, '#888', train[60]);
      train[61] = [];
      routeCount[61] = [];
      routeCount[61]['route_id'] = 61;
      routeCount[61]['count'] = drawTrack(639, 314, 675, 298, '#888', train[61]);
      train[62] = [];
      routeCount[62] = [];
      routeCount[62]['route_id'] = 62;
      routeCount[62]['count'] = drawTrack(461, 304, 490, 232, '#555', train[62]);
      train[63] = [];
      routeCount[63] = [];
      routeCount[63]['route_id'] = 63;
      routeCount[63]['count'] = drawTrack(490, 232, 513, 230, '#555', train[63]);
      train[64] = [];
      routeCount[64] = [];
      routeCount[64]['route_id'] = 64;
      routeCount[64]['count'] = drawTrack(573, 270, 513, 230, '#555', train[64]);
      train[65] = [];
      routeCount[65] = [];
      routeCount[65]['route_id'] = 65;
      routeCount[65]['count'] = drawTrack(573, 270, 596, 240, '#888', train[65]);
      train[66] = [];
      routeCount[66] = [];
      routeCount[66]['route_id'] = 66;
      routeCount[66]['count'] = drawTrack(596, 240, 560, 170, '#888', train[66]);
      train[67] = [];
      train[67][0] = [];
      train[67][0]['player_num'] = 1;
      train[67][0]['line'] = 'RED';
      train[67][0]['track_unit'] = 8;
      train[67][0]['direction'] = '+';
      train[67][0]['dest_city'] = 3;
      train[67][0]['status'] = 'ARRIVED';
      train[67][0]['route_id'] = 67;
      train[67][0]['goods'] = [];
      train[67][0]['goods'][0] = [];
      train[67][0]['goods'][0]['name'] = 'Timber';
      train[67][0]['goods'][0]['goods_id'] = 29;
      routeCount[67] = [];
      routeCount[67]['route_id'] = 67;
      routeCount[67]['count'] = drawTrack(513, 230, 528, 150, '#555', train[67]);
      train[68] = [];
      routeCount[68] = [];
      routeCount[68]['route_id'] = 68;
      routeCount[68]['count'] = drawTrack(528, 150, 560, 170, '#888', train[68]);
      train[69] = [];
      routeCount[69] = [];
      routeCount[69]['route_id'] = 69;
      routeCount[69]['count'] = drawTrack(560, 170, 567, 112, '#888', train[69]);
      train[70] = [];
      routeCount[70] = [];
      routeCount[70]['route_id'] = 70;
      routeCount[70]['count'] = drawTrack(567, 112, 553, 38, '#555', train[70]);
      train[71] = [];
      routeCount[71] = [];
      routeCount[71]['route_id'] = 71;
      routeCount[71]['count'] = drawTrack(560, 170, 661, 61, '#888', train[71]);
      train[72] = [];
      routeCount[72] = [];
      routeCount[72]['route_id'] = 72;
      routeCount[72]['count'] = drawTrack(553, 38, 661, 61, '#555', train[72]);
      train[73] = [];
      routeCount[73] = [];
      routeCount[73]['route_id'] = 73;
      routeCount[73]['count'] = drawTrack(344, 350, 425, 340, '#888', train[73]);
      train[74] = [];
      routeCount[74] = [];
      routeCount[74]['route_id'] = 74;
      routeCount[74]['count'] = drawTrack(425, 340, 461, 304, '#888', train[74]);
      train[75] = [];
      routeCount[75] = [];
      routeCount[75]['route_id'] = 75;
      routeCount[75]['count'] = drawTrack(425, 340, 499, 350, '#555', train[75]);
    </script>

解决方案

Use JSON. see why JSON over XML: http://thephpcode.blogspot.com/2008/08/why-json-over-xml-in-ajax.html

To use json in PHP simply:

<?php

  echo '<script type="text/javascript">/* <![CDATA[ */';
  echo 'var train = '.json_encode($array);
  echo '/* ]]> */</script>';

?>

the variable train in Javascript will be an object containing properties and arrays based on your PHP variable $array.

To parse or iterate through the train object, you can use for ... in statements for JSON arrays, and directly use object.property for JSON objects.

See this:

<?php

  $array = array(array('id'=>3,'title'=>'Great2'),array('id'=>5,'title'=>'Great'),array('id'=>1,'title'=>'Great3'))
  echo '<script type="text/javascript">/* <![CDATA[ */';
  echo 'var train = '.json_encode($array);
  echo '/* ]]> */</script>';

?>

The output will be:

 var train = [{id:3,title:'Great2'},{id:5,title:'Great'},{id:1,title:'Great3'}];

the variable train becomes an array of objects. [] squrare brackets are arrays, holding more arrays or objects. {} curly braces are objects, they have properties to them.

So to iterate the train variable:

<script type="text/javascript">

  var train = [{id:3,title:'Great2'},{id:5,title:'Great'},{id:1,title:'Great3'}];

  for(i in train){
    var t = train[i]; // get the current element of the Array
    alert(t.id); // gets the ID
    alert(t.title); // gets the title
  }

</script>

Simple! Hope that really helps.

这篇关于对于PHP数组转换为JavaScript的最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆