dsrjs86444 2016-12-19 14:53
浏览 98
已采纳

PHP中的反向数组,索引错误

In PHP, I have an array like this:

Array ( 
    [12] => Dec 
    [01] => Jan 
    [02] => Feb 
    [03] => Mar 
    [04] => Apr 
    [05] => May 
    [06] => Jun 
    [07] => Jul 
    [08] => Aug 
    [09] => Sep 
    [10] => Oct 
    [11] => Nov 
) 

I then do something like this: $reverse_months = array_reverse($months);

What I don't understand is why the result coming out like this:

Array ( 
    [0] => Nov 
    [1] => Oct 
    [09] => Sep 
    [08] => Aug 
    [07] => Jul 
    [06] => Jun 
    [05] => May 
    [04] => Apr 
    [03] => Mar 
    [02] => Feb 
    [01] => Jan 
    [2] => Dec 
)

You can clearly see the last three months of the years got wrong indexes compare to original $months array :-(

Could anyone is kind enough to explain why this weird behavior happens and how can I fix it please? The outcome result is my desirable outcome except the wrong array index for month Oct, Nov and Dec. Thank you!

  • 写回答

1条回答 默认 最新

  • dqe9657 2016-12-19 14:58
    关注

    10, 11 and 12 are numeric keys, where 01, 02 and so on are named keys, what php does is reenumerating the numbered keys. There are 3 of them so 0,1,2. Named keys are not reenumerated (How should they ?). To also preserve integer keys, use

    array_reverse($months, true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀