dongsheng1698 2014-05-10 04:37
浏览 45
已采纳

为什么这个函数不能修改数组的全局版本?

I have defined a variable in what I think is global scope. I want to modify the same variable inside a function that lives in a class, then use it later to export as json data.

The function is called by the xataface api, so I'm not sure I can mess with the function signature to do something like passing by reference. I've thought I might access the instance of this action class in the javascript embedded php, but I don't know how to ask the api for it, nor am I confident of its lifetime. It seems like a global variable may be the way to go. In any case, I want to know:

  1. Why is not the global instance of $dataset1 the one being modified inside the function?
  2. Why doesn't the call to array_push put anything on either array?
<?php
//non-dynamic data delcared in global scope. This is picked up later
//in a php block embedded into javascript
$dataset1 = array(array("label"=>"c120","data"=>"1"),
                  array("label"=>"c150","data"=>"10"),
                  array("label"=>"camp","data"=>"7"));
class actions_time_in_type
{
  function handle(&$params)
  {
    $this->app =& Dataface_Application::getInstance(); 
    //The Query
    $result = mysql_query("SELECT typeDes, total
                           FROM myTable", $this->app->db());
    //reserch leads me to believe that this *should* make all subsequent
    //references to $dataset1 use the global instance
    global $dataset1;
    //experimenting with appending more non-dynamic data
    //for some reason, this syntax does not seem to touch $dataset1 
    array_push($dataset1, array("label"=>"dv20","data"=>"1"));
    //This syntax is working, but $dataset1 is not the same as the global 
    //$dataset1. Prepending "global" here seems to crash the script
    $dataset1[] = array("label"=>"pa18","data"=>"5");
    while($row = mysql_fetch_assoc($result))
    {
       //append data to the array, again, this is not hitting
       //the global instance of $dataset1
       $dataset1[] = array("label"=>$row['typedes'],"data"=>$row['total']);
    }
    mysql_free_result($result); //Frees the result after finished using it           
    //diagnostic dump to see what we've got
    //This shows that we've constructed the dynamic data set, but it
    //seems to be scoped only to this function and does not make it into
    //javascript.
    var_dump($dataset1);
  }
}
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" ></script>
<script type="text/javascript">
$(function () {
   //This is getting only what was done original init of $dataset1, nothing that
   //happened in the function made a difference
   var dataset1 = <?php echo json_encode($dataset1); ?>;
});
</script>
  • 写回答

1条回答 默认 最新

  • douxie2029 2014-05-26 03:01
    关注

    It appears this is indeed a problem with execution order. Since the manipulation of the data happens within a class definition which in turn is called by the api, there appears to be no assurance that the global definition of the data will be in scope when it is time to manipulate the data, or that it will have been manipulated when the data is re-used further down.

    For the interested reader, I was able to get the api developer to demonstrate the proper way of integrating javascript with the api calling the in-class function:

    https://groups.google.com/forum/#!topic/xataface/l6qBzxF1vrc

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法