php - 如何从提取函数()将数组插入 $_SESSION()?
问题描述
我的 home.php 文件中有一个函数get_teacher_info()
。它从 mysql 查询返回一个数组。我将它存储在$data
变量中。
现在我想将'key'=>'value'
数组中的每一对设置为单独的变量。所以我正在使用extract()
如何设置我从中接收的会话变量extract()
我的代码如下。
$data = $teacher->get_teacher_info();
$_SESSION[]= extract($data);
解决方案
你可以像这样使用它
$data = $teacher->get_teacher_info();
// if there is addtional data in the `$_SESSION` before assigning data extracted from DB you can use the `array_merge`, this will keep previous data from `$_SESSION`
$_SESSION = array_merge($_SESSION, $data);
// If there is no additional data then directly assign the data from DB to `$_SESSION` only if the `$data` is already an associative array
$_SESSION = $data;
推荐阅读
- java - 如果我的 java home 是空的,maven 从哪里获取 java 版本
- apache-kafka - Spring Kafka 使用 JsonNode 对象
- python - 搜索字典列表并不能给出可靠的结果?
- python - aiortc - 更改视频流大小
- javascript - Javascript无法读取数组的第二个元素
- javascript - React 基于登录注销和表单提交的条件渲染
- php - Laravel 404 JSON 响应在 HTTP 和 HTTPS 中返回不同的消息
- hangfire - 当 Hangfire 服务器是单独进程的一部分时监控 UI?
- php - Symfony 捆绑配置服务参数
- python - 如何将 Sqlalchemy ORM 查询结果转换为包含关系的单个联接表?