2009/08/27

Remove a member from array and ArrayObject

For arrayobject, you can do
foreach ($arrobj as $obj){
do something ...
}
Like array does.

However you cannot use unset to remove a ArrayObject member, for example,
$hits is ArrayObject,

You could not do something like,
foreach ($hits as $hit) {
if(!preg_match("/.*abc$/", $hit->sku)){
unset($hits[$hit]);
}
}

You have to use ArrayObject's method called append to do it, like,

$new_hits = new ArrayObject();
foreach ($hits as $hit) {
if(!preg_match("/.*CR$/", $hit->sku)){
$new_hits->append($hit);
}
}
$hits = $new_hits;

No comments:

Post a Comment