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
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
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
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
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 )
Using array_filter() and a Callback Function
The array_filter() function can be used to filter elements of an array using a callback function. By using a callback function, we can effectively remove the middle element by skipping it during the filtering process.
- Calculate the number of elements in the array.
- Find the middle index using the floor function to handle both even and odd lengths.
- Use array_filter() with a callback function to exclude the middle element.
Example: This example shows the deletion of the middle element using array_filter() function.
<?php
function deleteMid($arr) {
$count = count($arr);
if ($count === 0) {
return $arr;
}
$midIndex = (int)floor(($count - 1) / 2);
$newArr = array_filter($arr, function($value, $index) use ($midIndex) {
return $index !== $midIndex;
}, ARRAY_FILTER_USE_BOTH);
return array_values($newArr);
}
$arr = [1, 2, 3, 4, 5];
$newArr = deleteMid($arr);
print_r($newArr);
?>
Output
Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 )