草庐IT

php - 将 PHP 数组转换为 javascript 的最佳方法

coder 2024-04-08 原文

我正在将一个 PHP 数组传递给一个 javascript 函数。我知道这样做的唯一方法是从 PHP 数组创建一个 js 数组并将其传递给 js 函数。但这会创建大量要传输的代码(请参阅下面的代码 - 还有很多,但我相信您已经了解了总体思路)。我怀疑有更有效的方法。有人愿意与我分享吗?谢谢!

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

最佳答案

json_encode。参见 http://us3.php.net/manual/en/function.json-encode.php

关于php - 将 PHP 数组转换为 javascript 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1387149/

有关php - 将 PHP 数组转换为 javascript 的最佳方法的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  5. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  6. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  7. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  8. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  9. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  10. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

随机推荐