I have multiple record for the session $_SESSION["cart_array"]
like
$_SESSION["cart_array"] = array(0 => array("item_id" => sms, "quantity" => $pe, "to" =>$to, "msg" => $message));
please see here https://ideone.com/NZysQc
In my achievement I was trying to output this record in a different page but it output only one record. What is mine doing wrong? This is my tried code:
foreach ($_SESSION["cart_array"] as $each_item) {
$id = $each_item['item_id'];
$to = $each_item['to'];
echo '$to and $id';
}
But it return only one record in the session.
My suggestion would be as such:-
$_SESSION["cart_array"][] = array("item_id" => $sms, "quantity" => $pe, "to" => $to, "msg" => $message);
to form the array and
foreach ($_SESSION["cart_array"] as $each_item) {
$id = $each_item['item_id'];
$to = $each_item['to'];
echo "$to and $id";
}
for the loop, notice the double quotes in the echo
.
Change
echo '$to and $id';
For:
echo "$to and $id";
As vars are not parsed inside simple quoted strings.
Your example has only the element 0, so only one element will be shown.
You could array_push your elements to you cart session var to have more than one element. And set it to the new array only if the var is stil not set.
$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(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>"; } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.