doudou201701 2014-04-11 14:22
浏览 15

禁用$ _SESSION数组内的排序

I have a session variable which looks like this when printed:

Array
(
    [cart_4] => 1
    [cart_8] => 1
    [cart_9] => 2
    [cart_18] => 1
)

The thing is, i did not add stuff to it in that order, but because of the keys it always has that order. So is it possible to keep the items inside the session array the way they were added, not like this?

In this particular example, I added cart_8 last, and as you can see it is second in the list.

  • 写回答

1条回答 默认 最新

  • douxian4323 2014-04-11 14:25
    关注

    You must be splicing elements in or sorting the array. The order of an array is the order in which the elements were were added:

    $a['cart_9'] = 2;
    $a['cart_18'] = 1;
    $a['cart_4'] = 1;
    $a['cart_8'] = 1;
    
    print_r($a);
    
    Array
    (
        [cart_9] => 2
        [cart_18] => 1
        [cart_4] => 1
        [cart_8] => 1
    )
    

    Even for numerically indexed arrays:

    $a[9] = 2;
    $a[18] = 1;
    $a[4] = 1;
    $a[8] = 1;
    
    print_r($a);
    
    Array
    (
        [9] => 2
        [18] => 1
        [4] => 1
        [8] => 1
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测