我有多个会议记录$_SESSION["cart_array"]
喜欢
$_SESSION["cart_array"] = array(0 => array("item_id" => sms, "quantity" => $pe, "to" =>$to, "msg" => $message));
在我的成就中,我试图在不同的页面中输出此记录,但它只输出一条记录。我做错了什么?这是我尝试过的代码:
foreach ($_SESSION["cart_array"] as $each_item) {
$id = $each_item['item_id'];
$to = $each_item['to'];
echo '$to and $id';
}
但它只返回会话中的一条记录。
更改
echo '$to and $id';
对于:
echo "$to and $id";
因为vars没有在简单的引用字符串中解析。
您的示例只有元素0,因此只显示一个元素。
你可以array_push你的元素你购物车会话var有多个元素。仅当var未设置时才将其设置为新数组。
$newItem = array("item_id" => sms, "quantity" => $pe, "to" =>$to, "msg" => $message);
if (empty($_SESSION["cart_array"]))
$_SESSION["cart_array"] = array(0 => $newItem);
else
array_push($_SESSION["cart_array"], $newItem);
我的建议如下: -
$_SESSION["cart_array"][] = array("item_id" => $sms, "quantity" => $pe, "to" => $to, "msg" => $message);
形成数组和
foreach ($_SESSION["cart_array"] as $each_item) {
$id = $each_item['item_id'];
$to = $each_item['to'];
echo "$to and $id";
}
对于循环,请注意echo
中的双引号。
$_SESSION["cart_array"] = array(0 => array("item_id" => sms, "quantity" => $pe, "to" =>$to, "msg" => $message),1 => array("item_id" => sms, "quantity" => $pe, "to" =>$to, "msg" => $message));
foreach($_SESSION["cart_array"] as $each_item_array)
{
foreach ($each_item_array as $each_item)
{
$id = $each_item['item_id'];
$to = $each_item['to'];
echo "$to and $id </br>";
}
}