php - PHP如何从数组中获取特定值并存储在`$_SESSION`中
问题描述
我有一个数组,我想在其中获取特定值('created'),然后将 im a 存储$_SESSION
在一个新数组中。我怎样才能做到这一点?
我的数组看起来像这样
Array
(
[0] => Array
(
[product_id] => 679
[quantity] => 1
[created] => 2018-10-01
)
[1] => Array
(
[product_id] => 677
[quantity] => 1
[created] => 2018-10-05
)
[2] => Array
(
[product_id] => 678
[quantity] => 1
[created] => 2018-10-03
)
)
我试过类似的东西:
foreach($created as $i) {
$values = [$i]['created'];
echo $values;
}
session_start();
$_SESSION['creation_dates'] = $values;
但这不起作用。
有人可以帮我吗?
解决方案
在任何输出之前,您首先需要开始您的会话。你echo
的 s 正在阻止这种情况。您必须得到Notices
但没有看到它们,具体取决于您的error_reporting
.
试试这个代码:
session_start();
$values = array();
foreach($creation_dates as $i) {
$values[] = $i['created'];
}
$_SESSION['creation_dates'] = $values;
print_r($values);
print_r($_SESSION['creation_dates']);
推荐阅读
- r - 量化斜率的重要性
- r - R - 面板数据 FE,没有唯一的时间 id 匹配,创建新的时间变量
- oracle - 为什么我在下面的代码中收到错误?
- docker - 无法连接到 Windows 上的 Docker 容器
- filter - 在组件内部运行的自定义过滤器和/或插件
- python - Python:将带有 Unicode 的字符串转换为 HTML 数字代码
- c# - 为什么 FindNextFile 在我的 C# 代码中不起作用?
- java - ANTLR4 从函数返回值
- php - 使用 PDO、MVC 和 PHP 将变量绑定到数据库查询的问题
- python - 如何使用 DataFrame 编辑 Excel 文件并将其另存为 Excel 文件?