PHP Program to Delete Middle Element from an Array
Last Updated :
31 Jul, 2024
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:
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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
)
Similar Reads
PHP | Program to delete an element from array using unset() function Given an array of elements, we have to delete an element from the array by using the unset() function. Examples: Input : $arr = array("Harsh", "Nishant", "Bikash", "Barun"); unset($arr[3]); Output : Array ( [0] => Harsh [1] => Nishant [2] => Bikash ) Input : $arr = array(1, 2, 6, 7, 8, 9);
2 min read
How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i
4 min read
PHP | Remove duplicate elements from Array You are given an Array of n-elements.You have to remove the duplicate values without using any loop in PHP and print the array. Examples: Input : array[] = {2, 3, 1, 6, 1, 6, 2, 3} Output : array ( [6] => 2 [7] => 3 [4] => 1 [5] => 6 ) Input : array[] = {4, 2, 7, 3, 2, 7, 3} Output : arr
3 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read
How to delete an element from an array using JavaScript ? The array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index. In this article, we will discuss different ways to remove elements from the array.
5 min read
PHP | Deleting an element from array using array_diff() Given an array, we have to delete an element from that array using the array_diff() function in PHP.Examples:Input : $arr = array(2, 8, 9, 7, 6, 5); $arr = array_diff($arr, array(9)); Output : Array ( [0] => 2 [1] => 8 [3] => 7 [4] => 6 [5] => 5 ) Input : $arr = array("shubham", "aksh
2 min read
Removing Array Element and Re-Indexing in PHP In order to remove an element from an array, we can use unset() function which removes the element from an array, and then use array_values() function which indexes the array numerically automatically. Function Usedunset(): This function unsets a given variable. Syntax:void unset ( mixed $var [, mix
2 min read
PHP | Separate odd and even elements from array without using loop You are given an array of n elements in PHP. You have to separate the elements from the array based on the elements are odd or even. That is, print odd array and even array separately without traversing the original array or using any loop. Examples: Input : array(2, 5, 6, 3, 0) Output : Odd array:
3 min read
Deleting Elements in an Array - Array Operations In this post, we will look into deletion operation in an Array, i.e., how to delete an element from an Array, such as:Delete an Element from the Beginning of an ArrayDelete an Element from a Given Position in an ArrayDelete First Occurrence of Given Element from an ArrayRemove All Occurrences of an
4 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read