2009-09-07 53 views
4

我正在向PHP函数传递一个PHP数组。我知道这样做的唯一方法是从PHP数组创建一个js数组并将其传递给js函数。但是这会创建一大块代码来传输(请参阅下面的代码 - 还有很多,但我相信你会得到一般想法)。我怀疑有一种更有效的方法。有人愿意与我分享吗?谢谢!将PHP数组转换为javascript的最佳方法

<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> 

回答

9

使用JSON。为什么JSON在XML:http://thephpcode.blogspot.com/2008/08/why-json-over-xml-in-ajax.html

在PHP中只使用JSON:

<?php 

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

?> 

JavaScript中的变量train将包含基于你的PHP变量$array性能和数组的对象。

要解析或遍历列表对象,可以使用for ... in语句作为JSON数组,并直接使用object.property作为JSON对象。

参见此:

<?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>'; 

?> 

的输出将是:

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

可变train成为对象的数组。 [] squrare括号是数组,持有更多的数组或对象。 {}大括号是对象,它们具有属性。

所以要遍历train变量:

<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> 

简单!希望真的有帮助。

+1

谢谢Mauris!你的代码示例非常有用。我想知道如何将js中的PHP数组转换为JSON,现在我知道了。 – 2009-09-07 15:39:48

+0

嘿克里斯,一点问题都没有! – mauris 2009-09-07 16:01:48

+0

Hi Marius:现在我在我的js中有了这个JSON对象,我该如何迭代它?你能给我一个这样的代码示例吗?谢谢! – 2009-09-07 16:03:52

5

编码JavaScript数据结构的更节省空间的方法称为JSON

从PHP 5.2.0起,您可以使用以下函数来吐出一些JSON:json_encode()

相关问题