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

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 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架