dongque20030402 2016-04-28 16:31
浏览 41
已采纳

如何在php中将数组赋值给变量

I have a function with a reference parameter. The function populates the reference with an array. When it returns I test the reference value with a print_r(). It's successful. But when I try and assign it to another var as in,

$_SESSION['allvats'] = $ref_all_vats;

I get an empty session var. How do I assign this? I've tried declaring the reference as an array before invoking the function and also assigning to sessions as

$_SESSION['allvats'][] = $ref_all_vats;

Thank you.

Edit.

Session is running. Other SESSION vars populate. Here is the code bit:

    if(buildMetalArrayNsp($process_id, $ref_all_vats)) {
       writeDataToFile("vats " . print_r($ref_all_vats, true), __FILE__, __LINE__);
       $_SESSION['allvats'] = $ref_all_vats;
    }

Here is the session code:

$lifetime= 60 * 60 * 24 * 30;  // 30 days
session_start();
setcookie(session_name(),session_id(),time()+$lifetime);
  • 写回答

3条回答 默认 最新

  • dongmimeng5500 2016-04-28 16:59
    关注

    Your code is fine, there is something else going wrong. Are you sure the if statement is evaluating to true? Whenever you are tracking down errors it is good to use echo or print_r quite a bit. For example:

    echo "<pre>Above if
    ";
    if(buildMetalArrayNsp($process_id, $ref_all_vats)) {
       echo "In if
    ";
       $_SESSION['hello'] = "world";
       $_SESSION['allvats'] = $ref_all_vats;
    }
    echo "Below if
    ";
    
    echo "
    -----------------------
    ";
    echo $_SESSION['hello'];
    echo "
    -----------------------
    ";
    print_r($_SESSION['allvats']);
    echo "
    -----------------------
    ";
    print_r($ref_all_vats);
    echo "</pre>";
    

    Of course if you are not displaying to a web page remove the <pre> tags.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘