i wrote an array wrapper class PersonArray which can contain objects of a certain type (Person). Every person has a unique getHash() function which returns the ID + Name as a unique identifier. This allows for speedy retrieval of the Person from the PersonArray. The PersonArray actually holds two internal Arrays. One for the storage of Person objects ($items), and one for the storage of the Hash values ($itemsHash).
我写了一个数组包装类PersonArray,它可以包含某种类型的对象(Person)。每个人都有一个唯一的getHash()函数,它返回ID + Name作为唯一标识符。这允许从PersonArray中快速检索Person。 PersonArray实际上拥有两个内部数组。一个用于存储Person对象($ items),另一个用于存储哈希值($ itemsHash)。
I want to create a insertAt(index, Person) function which puts the Person object at the [index] position in the $items array. Is there a way to insertAt a certain position in an array? If so how can I also update the $itemsHash of the PersonArray?
我想创建一个insertAt(index,Person)函数,它将Person对象放在$ items数组的[index]位置。有没有办法在数组中插入某个位置?如果是这样,我怎样才能更新PersonArray的$ itemsHash?
class Person {
function getHash() {
return $this->id . $this->name;
}
}
class PersonArray implements Iterator {
public $items = array();
public $itemsHash = array();
public function Find($pKey) {
if($this->ContainsKey($pKey)) {
return $this->Item($this->internalRegisteredHashList[$pKey]);
}
}
public function Add($object) {
if($object->getHash()) {
$this->internalRegisteredHashList[$object->getHash()] = $this->Count();
array_push($this->items, $object);
}
}
public function getItems() {
return $this->items;
}
function ContainsKey($pKey) {}
function Count() {}
function Item($pKey) {}
//Iteration implementation
public function rewind() {}
public function current() {}
public function key() {}
public function next() {}
public function valid() {}
}
1 个解决方案
#1
You may find it is faster and easier to use PHP's associative arrays rather than re-implementing them.
您可能会发现使用PHP的关联数组更快更容易,而不是重新实现它们。
As an aside you can also implement the simpler IteratorAggregate
if you are actually just iterating over an array.
另外,如果您实际上只是迭代数组,那么您也可以实现更简单的IteratorAggregate。
e.g.
class PersonArray implements IteratorAggregate {
public $items = array();
public function getItems() {
return $this->items;
}
public function Add($object) {
if($object->getHash()) {
$this->items[$object->getHash()] = $object;
}
}
public function Find($pKey) {
if(isset($this->items[$pKey])) {
return $this->items[$pKey];
}
}
public function insertAt($index, $person) {
$tmp = array_slice($this->items, 0, $index);
$tmp[$person->getHash()] = $person;
$tmp = array_merge($tmp, array_slice($this->items, $index));
$this->items = $tmp;
}
//IteratorAggregate implementation
public function getIterator() {
return new ArrayIterator($this->items);
}
}
#1
You may find it is faster and easier to use PHP's associative arrays rather than re-implementing them.
您可能会发现使用PHP的关联数组更快更容易,而不是重新实现它们。
As an aside you can also implement the simpler IteratorAggregate
if you are actually just iterating over an array.
另外,如果您实际上只是迭代数组,那么您也可以实现更简单的IteratorAggregate。
e.g.
class PersonArray implements IteratorAggregate {
public $items = array();
public function getItems() {
return $this->items;
}
public function Add($object) {
if($object->getHash()) {
$this->items[$object->getHash()] = $object;
}
}
public function Find($pKey) {
if(isset($this->items[$pKey])) {
return $this->items[$pKey];
}
}
public function insertAt($index, $person) {
$tmp = array_slice($this->items, 0, $index);
$tmp[$person->getHash()] = $person;
$tmp = array_merge($tmp, array_slice($this->items, $index));
$this->items = $tmp;
}
//IteratorAggregate implementation
public function getIterator() {
return new ArrayIterator($this->items);
}
}