What is an elegant way to remove an object from an array of objects in PHP ?
Just to be clear ..
class Data{ private $arrObservers; public add(Observer $o) { array_push($this->arrObservers, $o); } public remove(Observer $o) { // I NEED THIS CODE to remove $o from $this->arrObservers } }
There are different methods and techniques you can use to remove elements from JavaScript arrays: pop - Removes from the End of an Array. shift - Removes from the beginning of an Array. splice - removes from a specific Array index.
An object is an instance of a class. Using the PHP unset() function, we can delete an object. So with the PHP unset() function, putting the object that we want to delete as the parameter to this function, we can delete this object.
To remove an object from an array by its value:Call the findIndex() method to get the index of the object in the array. Use the splice() method to remove the element at that index. The splice method changes the contents of the array by removing or replacing existing elements.
You can do
function unsetValue(array $array, $value, $strict = TRUE) { if(($key = array_search($value, $array, $strict)) !== FALSE) { unset($array[$key]); } return $array; }
You can also use spl_object_hash
to create a hash for the objects and use that as array key.
However, PHP also has a native Data Structure for Object collections with SplObjectStorage
:
$a = new StdClass; $a->id = 1; $b = new StdClass; $b->id = 2; $c = new StdClass; $c->id = 3; $storage = new SplObjectStorage; $storage->attach($a); $storage->attach($b); $storage->attach($c); echo $storage->count(); // 3 // trying to attach same object again $storage->attach($c); echo $storage->count(); // still 3 var_dump( $storage->contains($b) ); // TRUE $storage->detach($b); var_dump( $storage->contains($b) ); // FALSE
SplObjectStorage
is Traversable
, so you can foreach
over it as well.
On a sidenote, PHP also has native interfaces for Subject and Observer.
I agree with the answers above, but for the sake of completeness (where you may not have unique IDs to use as a key) my preferred methods of removing values from an array are as follows:
/** * Remove each instance of a value within an array * @param array $array * @param mixed $value * @return array */ function array_remove(&$array, $value) { return array_filter($array, function($a) use($value) { return $a !== $value; }); } /** * Remove each instance of an object within an array (matched on a given property, $prop) * @param array $array * @param mixed $value * @param string $prop * @return array */ function array_remove_object(&$array, $value, $prop) { return array_filter($array, function($a) use($value, $prop) { return $a->$prop !== $value; }); }
Which are used in the following way:
$values = array( 1, 2, 5, 3, 5, 6, 7, 1, 2, 4, 5, 6, 6, 8, 8, ); print_r(array_remove($values, 6)); class Obj { public $id; public function __construct($id) { $this->id = $id; } } $objects = array( new Obj(1), new Obj(2), new Obj(4), new Obj(3), new Obj(6), new Obj(4), new Obj(3), new Obj(1), new Obj(5), ); print_r(array_remove_object($objects, 1, 'id'));
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With