drslez4322 2017-02-27 21:53
浏览 443
已采纳

PHP:保留array_values的顺序

If I have an associative array that is structured like

(
    1 => 'a',
    2 => 'b',
    0 => 'c'
)

where all of the keys are numeric, will array_values ALWAYS guarantee that the values occur chronologically, in the new array, based on their previous keys' values, i.e. ['c', 'a', 'b']?

If not, how can I accomplish this instead?

  • 写回答

2条回答 默认 最新

  • dsdeeaquu38538545 2017-02-27 22:02
    关注

    No, array_values() will not reorder the values in any way. It doesn't care about keys.

    Its effective implementation is basically this:

    function array_values_impl(array $array)
    {
        $newArray = [];
    
        foreach ($array as $item) {
            $newArray[] = $item;
        }
    
        return $newArray;
    }
    

    If you want to sort the array using the keys, use ksort().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件