【问题标题】:Remove a value from Session array从 Session 数组中删除一个值
【发布时间】:2015-12-19 05:13:08
【问题描述】:

如何使用表中的删除链接从会话数组中删除一个值。

//在文件A中

< ?php
$_SESSION['cart']['prices'][] = "1000";
$_SESSION['cart']['services'][] = "game 1";
$_SESSION['cart']['prices'][] = "2000";
$_SESSION['cart']['services'][] = "game 2";
$_SESSION['cart']['prices'][] = "3000";
$_SESSION['cart']['services'][] = "game 3";
$_SESSION['cart']['prices'][] = "4000";
$_SESSION['cart']['services'][] = "game 4";
?>

//在文件B中

< table border="1">
< thead>
< th style="width: 450px; text-align:center;">Services< /th>
< th style="width: 450px; text-align:center;">Price< /th>
< th style="width: 450px; text-align:center;">Delete item< /th>
< /thead>
< tbody>
< ?php
foreach ($_SESSION['cart']['services'] as $key => $service) {
    echo "< tr>< td>".$service."< /td>< td style='text-align:center;'>".$_SESSION['cart']['prices'][$key]."< /td>< td>ckick to remove< /td>< /tr>";
}
?>
< /tbody>
< /table>

【问题讨论】:

  • 用'unset()'试试?
  • 例如unset($_SESSION['cart']['services']);
  • 更好unset($_SESSION['cart']['services'][$indexToRemove]);
  • 但我想删除特定的服务和价格。怎么可能?'
  • 只需使用$_SESSION['cart']['services'][$index] = null;unset($_SESSION['cart']['services']);

标签: php arrays session


【解决方案1】:

使用unset() 重置会话值:

unset($_SESSION['cart']['services']);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2013-02-19
    相关资源
    最近更新 更多