In this tutorial, you will learn how to remove elements or values from an array in PHP with examples.
How to Remove elements,values, and array from array in PHP
Here, you will explore some of the most popular ways to remove values from arrays in PHP:
- Method 1: Using unset() function
- Method 2: Using array_diff() function
- Method 3: Using array_splice() function
- Method 4: Using array_pop() function
Method 1: Using unset() function
The unset() function is used to remove an element from an array. The syntax for using unset() function to remove an element from an array is as follows:
unset($array[index]);
Here, $array
is the name of the array, and index
is the index of the element you want to remove.
Let’s take an example. Suppose you have an array named $fruits
that contains the following elements:
$fruits = array("apple", "banana", "orange", "kiwi", "mango");
Now, let’s say you want to remove the element “kiwi” from the array. You can do so using the following code:
unset($fruits[3]);
After executing this code, the array $fruits
will contain the following elements:
array("apple", "banana", "orange", "mango");
Let’s take another example for removing an element from a one-dimensional array by key:
To remove an element from a one-dimensional array by key, you can use the unset() function along with the array key that want to remove. Here’s an example:
$fruits = array('apple' => 1, 'banana' => 2, 'orange' => 3, 'kiwi' => 4);
unset($fruits['orange']);
print_r($fruits);
Method 2: Using array_diff() function
Another way to remove values from an array is to use the array_diff() function. This function returns an array containing all the values of the first array that are not present in any of the other arrays.
The syntax for using array_diff() function to remove values from an array is as follows:
$new_array = array_diff($array, array(value1, value2, ...));
Here, $array
is the name of the array, and value1
, value2
, etc. are the values you want to remove.
Let’s take an example. Suppose you have an array named $numbers
that contains the following elements:
$numbers = array(1, 2, 3, 4, 5);
Now, let’s say you want to remove the values 2 and 4 from the array. You can do so using the following code:
$new_array = array_diff($numbers, array(2, 4));
After executing this code, the array $new_array
will contain the following elements:
array(1, 3, 5);
Method 3: Using array_splice() function
The array_splice() function is used to remove a portion of an array and replace it with something else. The syntax for using array_splice() function to remove values from an array is as follows:
array_splice($array, $start, $length);
Here, $array
is the name of the array, $start
is the index of the element where the removal should start, and $length
is the number of elements to remove.
Let’s take an example. Suppose you have an array named $colors
that contains the following elements:
$colors = array("red", "green", "blue", "yellow", "pink");
Now, let’s say you want to remove the elements “blue” and “yellow” from the array. You can do so using the following code:
array_splice($colors, 2, 2);
After executing this code, the array $colors
will contain the following elements:
array("red", "green", "pink");
Method 4: Using array_pop() function
The array_pop() function is a part of PHP’s array manipulation functions. It removes the last element of an array and returns it. This function modifies the original array and reduces its length by one. If the array is empty, array_pop() returns NULL.
Now, let’s look at some examples of using array_pop() to remove values from an array.
Example 1: Removing the last element from an array
Suppose you have an array of fruits, and you want to remove the last fruit from it. You can use the array_pop() function as follows:
$fruits = array('apple', 'banana', 'orange', 'kiwi');
$last_fruit = array_pop($fruits);
echo "Removed fruit: " . $last_fruit . "<br>";
print_r($fruits);
Example 2: Removing multiple elements from an array
Suppose you have an array of numbers, and want to remove the last two elements from it. You can use the array_pop() function twice, as follows:
$numbers = array(10, 20, 30, 40, 50);
array_pop($numbers);
array_pop($numbers);
print_r($numbers);
Conclusion
In summary, removing elements from arrays is a common task in PHP programming. Developers can use built-in functions such as array_pop(), array_shift(), and unset() to remove elements from regular arrays, and array_splice() and array_filter() for multi-dimensional arrays.