优化重新索引 ArrayObject

Optimize reindexing ArrayObject

我需要优化或自定义函数来更新对象扩展 ArrayObject 的索引

示例:

<?php

class MyCollection extends ArrayObject
{
    // my logic for collection
}

$collection = new MyCollection([
    'first',
    'second',
    'third',
]); // will output [0 => 'first', 1 => 'second', 2 => 'third']

$collection->offsetUnset(1); // will output [0 => 'first', 2 => 'third']

// some reindex function
$collection->updateIndexes(); // will output [0 => 'first', 1 => 'third']

使用exchangeArray to swap out the inner array with one that's been run through array_values。您可以将其组合成您自定义的方法 MyCollection class:

class MyCollection extends ArrayObject
{
  public function updateIndexes() {
    $this->exchangeArray(array_values($this->getArrayCopy()));
  }
}

https://3v4l.org/S8I7Z