PHP Program to Delete Middle Element from an Array

Given an array, the task is to delete the middle element of the array. If the array has an even number of elements, the middle element can be considered as the one closer to the start of the array (the lower middle).

These are the following approaches:

Table of Content

  • Using array_splice() function
  • Using array_slice() function
  • Using a Loop

Using array_splice() function

The array_splice() function removes elements from an array and can also replace them with new elements. This function is very handy for our task.

  • Calculate the number of elements in the array.
  • Find the middle index using the floor to handle both even and odd lengths.
  • Use array_splice() to remove the element at the middle index.

Example: This examples shows the deletion of the middle element using array_splice() function.

PHP
<?php

    function deleteMid($arr) {
        $count = count($arr);
        
        if ($count === 0) {
            return $arr;
        }

        $midIndex = (int)floor(($count - 1) / 2);
        array_splice($arr, $midIndex, 1);
        
        return $arr;
    }

    // Driver code
    $arr = [1, 2, 3, 4, 5];
    $newArr = deleteMid($arr);
    print_r($newArr);

?>

Output
Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
)

Using array_slice() function

The array_slice() function extracts a portion of an array. By combining slices of the array, we can effectively remove the middle element.

  • Calculate the number of elements in the array.
  • Find the middle index using the floor.
  • Use array_slice() to get the portions of the array before and after the middle element.
  • Merge these two parts using array_merge().

Example: This examples shows the deletion of the middle element using array_slice() function.

PHP
<?php

    function deleteMid($arr) {
        $count = count($arr);
        
        if ($count === 0) {
            return $arr;
        }

        $midIndex = (int)floor(($count - 1) / 2);
        $part1 = array_slice($arr, 0, $midIndex);
        $part2 = array_slice($arr, $midIndex + 1);
        
        return array_merge($part1, $part2);
    }

    // Driver code
    $arr = [1, 2, 3, 4, 5];
    $newArr = deleteMid($arr);
    print_r($newArr);

?>

Output
Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
)

Using a Loop

A loop can be used to create a new array without the middle element.

  • Calculate the number of elements in the array.
  • Find the middle index using floor.
  • Loop through the array and copy elements to a new array, skipping the middle element.

Example: This examples shows the deletion of the middle element using a loop.

PHP
<?php

    function deleteMid($arr) {
        $count = count($arr);
        
        if ($count === 0) {
            return $arr;
        }

        $midIndex = (int)floor(($count - 1) / 2);
        $newArr = [];
        
        for ($i = 0; $i < $count; $i++) {
            if ($i !== $midIndex) {
                $newArr[] = $arr[$i];
            }
        }        
        return $newArr;
    }

    // Driver code
    $arr = [1, 2, 3, 4, 5];
    $newArr = deleteMid($arr);
    print_r($newArr);

?>

Output
Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
)