doumo2501 2013-04-01 10:52
浏览 40
已采纳

将javascript变量设置为PHP数组的特定索引

I have a one (very) big PHP array (with 649 indexes). I want to set a javascript variable to specific index of it and that index will depend on another variable. Is there any way? I know i can copy whole PHP array into JavaScript Array using json_encode($phpArray) , but i don't want whole array to be loaded on client side (for speed and some sort of security).

 $phpArray[0] = "i am first element";
 $phpArray[1] = "i am second element";
 /* ......... */
 $phpArray[100] = "i am 100th element";

and now, let us say, i want to get second element of $phpArray ? how can i do it?

  • 写回答

1条回答 默认 最新

  • doumu6941 2013-04-01 10:59
    关注

    Here is an example using jQuery $.post method:

    $(document).ready(function(){
      var my_var;
      $.post("my-php-page.php",
             {my_index: 1},
             function(data) {
               my_var = data.my_value;
             },
             "json");
    });
    

    my-php-page.php

    <?php
     $phpArray[0] = "i am first element";
     $phpArray[1] = "i am second element";
     /* ......... */
     $phpArray[100] = "i am 100th element";
     echo json_encode(array("my_value" => $phpArray[$_POST['my_index']]));
    ?>
    

    Is this what you want?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页