duanli9569 2014-08-26 04:28 采纳率: 100%
浏览 41
已采纳

将$ _SESSION键转换为局部变量

Today I have an interesting problem, where I need to take data stored in $_SESSION['form'][$key] and convert it to a local variable.

For example, lets say $_SESSION['form'] contains the following session keys:

$_SESSION['form']['name']
$_SESSION['form']['email']
$_SESSION['form']['age']

How can I convert these $_SESSION variables to:

$name
$email
$age

I was thinking that a for loop would work well for this, but I am not sure how to do that properly for this situation.

Thank you for your help!

  • 写回答

3条回答 默认 最新

  • douhan9619 2014-08-26 04:36
    关注

    You need to use the PHP extract function.

    in your case it should be

    <?php
    
    $name = "its existing variable"; \
    ote this existing variable ;)
    
    extract($_SESSION['form'], EXTR_PREFIX_SAME, "from_session");
    
    echo "$from_session_name, $email, $age, $name
    ";
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?