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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog