例:
我有和像这样的数组:
数组([0] => Apple [2] =>橙[5] =>梨[8] =>梨)
有一个功能来完成缺失的索引:1,3,4,6,7 ????
对于较大的阵列,这应该更快.对于较小的阵列,任何方法都可以.
$existingKeys = array_keys($array);
//you can use any value instead of null
$newKeys = array_fill_keys(range(min($existingKeys), max($existingKeys)), null);
$array += $newKeys;
//optional, probably not needed
ksort($array);
Run Code Online (Sandbox Code Playgroud)