How to remove object from array?

How to remove object from array?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to remove object from array in PHP

When dealing with dynamic lists of objects, the most common stumbling block isn't finding the element, but determining how to verify its existence before attempting deletion. In your specific Data class scenario, you have an array of Observer objects stored in $this->arrObservers. The core challenge is distinguishing between a simple value lookup and an object identity check. If you try to use in_array($o, $this->arrObservers) without care, PHP will compare properties rather than instances, causing the removal logic to fail silently or throw unexpected errors depending on your PHP version and strict settings.

To solve this elegantly, you must ensure that the comparison checks for object identity. The standard approach involves iterating through the array to find the index of the specific instance you wish to remove, then using unset to splice it out. However, a more idiomatic solution in modern PHP leverages array_filter. This function allows you to define a callback that returns false for any element you want to discard. By passing your target $o into this filter, you can reconstruct the array containing only observers that are not identical to the one being removed.

Here is how you might implement the remove method using array_filter:

public function remove(Observer $o) { 
    // Filter out any observer that is identical to $o
    $this->arrObservers = array_filter($this->arrObservers, function ($item) use ($o) {
        return !($item === $o);
    });

    // Crucial step: reset keys to maintain a dense array (0, 1, 2...)
    $this->arrObservers = array_values($this->arrObservers);
}

The choice of the === operator is critical here. It performs strict identity comparison, meaning it checks if both variables point to the exact same memory address. Using == would compare the properties of the objects, which is rarely what you want when removing a specific instance from a collection. If your observers are simple data containers, this distinction matters immensely; if they are complex entities with mutable state, relying on property equality can lead to logic errors where valid objects get removed simply because their current state matches another object.

Another consideration is array key preservation. When you remove an element from the middle of an array using unset, PHP leaves a "hole" or null value at that index. While this doesn't break functionality immediately, it can cause issues later if you rely on sequential keys for iteration or offset access. The array_values() call in the example above re-indexes the remaining elements, ensuring your $arrObservers remains a clean, dense array.

If you prefer not to use array_filter, an alternative is using array_splice. You would first use array_search with the strict comparison flag (true) to find the index, then splice that single element out. This approach modifies the original array in place but requires handling the case where the object is not found to avoid warnings.

public function remove(Observer $o) { 
    $key = array_search($o, $this->arrObservers, true);
    if ($key !== false) {
        unset($this->arrObservers[$key]);
        // Re-index the array if you need dense keys
        $this->arrObservers = array_values($this->arrObservers);
    }
}

Both methods have their place. array_filter is often more readable for "keep everything except X" logic, while array_splice can be slightly more performant on very large arrays if you are confident the object exists. Regardless of the method chosen, always remember that removing an object by reference requires strict identity checks to ensure you are mutating the correct instance within your collection.

Our network