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

报告相同问题?