我需要一个优化或自定义函数来更新对象扩展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']发布于 2018-09-24 12:41:29
使用exchangeArray将内部数组替换为已通过array_values运行的数组。您可以将其组合为自定义MyCollection类上的方法:
class MyCollection extends ArrayObject
{
public function updateIndexes() {
$this->exchangeArray(array_values($this->getArrayCopy()));
}
}https://stackoverflow.com/questions/52479431
复制相似问题