douoyou3348 2016-04-16 04:10
浏览 56
已采纳

从会话数组中删除项后重新排序数组键[重复]

This question already has an answer here:

I have $_SESSION['items'] to stored cart items as structure below:

print_r($_SESSION['items']):

Array
(
[0] => Array
    (
        [p_name] => Germany Fully Synthetic Engine Oil 5w40, 4L
        [p_code] => 15177651
        [p_coverImg] => 13-1460446338-kMTXa.jpg
        [p_id] => 13
        [p_price] => 126.00
        [p_qty] => 2
    )

[1] => Array
    (
        [p_name] => BetterBody Foods Organic Cold Pressed Extra Virgin Coconut Oil
        [p_code] => 15599414
        [p_coverImg] => 9-1460445708-6XlfS.jpg
        [p_id] => 9
        [p_price] => 278.40
        [p_qty] => 5
    )

[2] => Array
    (
        [p_name] => X-Dot Motorbike Helmet G88 + Bogo Visor (Tinted)
        [p_code] => 2102649
        [p_coverImg] => 12-1460446199-wI5qx.png
        [p_id] => 12
        [p_price] => 68.00
        [p_alt-variation-1] => Blue
        [p_alt-variation-2] => L
        [p_qty] => 2
    )

)

so to remove item I use unset($_SESSION['items'][$arr_key]);

let says I unset($_SESSION['items'][0]); from above array, after all I print_r($_SESSION['items']), the array key is not starting with [0] anymore:

Array
(
[1] => Array
    (
        [p_name] => BetterBody Foods Organic Cold Pressed Extra Virgin Coconut Oil
        [p_code] => 15599414
        [p_coverImg] => 9-1460445708-6XlfS.jpg
        [p_id] => 9
        [p_price] => 278.40
        [p_qty] => 5
    )

[2] => Array
    (
        [p_name] => Wonder Belt Set (2pcs)
        [p_code] => 33134567
        [p_coverImg] => 2-1460445193-vZwGV.jpg
        [p_id] => 2
        [p_price] => 199.00
        [p_qty] => 1
    )

)

how can I restore these session array stating with key 0 again after remove certain item?

</div>
  • 写回答

3条回答 默认 最新

  • dousi7919 2016-04-16 04:19
    关注

    You should use array_values as

    array_values($_SESSION['items']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序