【问题标题】:remove session array element删除会话数组元素
【发布时间】:2015-09-26 22:34:00
【问题描述】:

如何获取索引以删除会话数组元素。 我想通过点击“点击删除”删除服务“game2”和相关价格 2000

//在文件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>

【问题讨论】:

    标签: php html arrays session


    【解决方案1】:

    用户array_search()$_SESSION['cart']['services']

    $key = array_search( 'game2', $_SESSION['cart']['services'] );
    /* And remove the associated price */
    unset( $_SESSION['cart']['prices'][ $key ] )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 2011-04-08
      • 2020-09-07
      • 1970-01-01
      相关资源
      最近更新 更多