Delete element from multidimensional-array based on value

Delete element from multidimensional-array based on value

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

Stefan Bogdanescu

Founder & Senior Architect

Filtering Multidimensional Arrays in PHP 5.2

You have a multidimensional array of film records and need to remove specific sub-arrays based on a value inside them, like filtering out entries where the year is 2011. In modern PHP environments, you might reach for array_filter() or simply iterate with a foreach loop. However, since you are running PHP 5.2, those elegant built-in tools often feel insufficient or require workarounds that don't exist in the standard library of that version.

The core challenge here is that array_splice does not have a filter callback option in PHP 5.2; it only accepts a start index and a length. If you try to pass a condition to it, the code will fail. To achieve this filtering behavior, you must manually construct a new array or manipulate indices directly.

The Manual Rebuild Approach

The most reliable method that works consistently across PHP 5.2 without relying on missing functions is to iterate through your source array and build a new one containing only the items you want to keep. This approach separates the logic of "which items match" from the data structure itself.

Here is how you can implement this:

$films = [
    [
        'filmId' => 61359,
        'year'   => 2011, // This needs to be removed
        'title'  => 'Unstoppable',
    ],
    [
        'filmId' => 87923,
        'year'   => 2011, // This also needs to be removed
        'title'  => 'Unstoppable',
    ],
    [
        'filmId' => 68593,
        'year'   => 2010, // Keep this
        'title'  => 'Unstoppable',
    ]
];

$keep = [];
foreach ($films as $film) {
    if ($film['year'] != 2011) {
        $keep[] = $film;
    }
}

This loop checks each sub-array. If the condition $film['year'] != 2011 is true, it appends that element to the $keep array. Once the loop finishes, $keep contains your filtered data. This is safe, readable, and strictly compatible with PHP 5.2 syntax.

Why Not array_splice?

It is tempting to think about using array_splice because you are literally removing elements from a list. However, array_splice operates on indices, not values. To use it effectively in this context, you would first have to create an index map of where the unwanted items are located.

In PHP 5.2, you could do something like this:

$indices = [];
foreach ($films as $index => $film) {
    if ($film['year'] == 2011) {
        $indices[] = $index;
    }
}

// Then iterate backwards to remove them safely
rsort($indices); // Sort indices in descending order

foreach ($indices as $index) {
    array_splice($films, $index, 1);
}

This works by identifying all the indices where the condition fails and then deleting those specific slots. Note that you must rsort the indices before deletion. If you delete index 3 first, the element previously at index 4 shifts down to index 3. If you haven't sorted in descending order, your next deletion attempt might target the wrong item or throw an error if you try to access an out-of-bounds index. This adds unnecessary complexity compared to the rebuild method.

Trade-offs and Considerations

The manual rebuild approach creates a new array rather than modifying the original in place. In PHP 5.2, memory management was less efficient than in modern versions, so creating a copy of large datasets could be costly. However, for typical application data sizes, this overhead is negligible compared to the cost of writing complex index-mapping logic.

Another consideration is that this method does not preserve the original keys if you were relying on them being sequential integers starting from 0. Since your example shows numeric keys ([0], [1]), and the rebuild approach uses [] which creates new numeric keys, the result will still be a clean zero-indexed array. If your data relied on specific non-sequential keys, you would need to assign them manually during the copy process.

If you are eventually moving away from PHP 5.2, you should note that in later versions (specifically PHP 5.4+), array_filter became significantly more powerful with support for callbacks allowing direct manipulation of values, though it still returns a new array by default unless using ARRAY_FILTER_USE_BOTH. For now, sticking to the explicit loop ensures your application remains stable on legacy infrastructure without unexpected behavior.

Our network