PHP

PHP Delete Item From Array Examples

Arrays, as always, have been a very important concept. While working with arrays, there might have been an instance where you wanted to delete an array element. In this article, I am going to share with you easy ways to use PHP to delete item from array by means of easy examples.

How To Delete Item From Array

Let’s  assume that we have an array of states. So we will see few examples that satisfy the following requirements:

1. Deleting an Array Element when array consists of unique values.

2. Deleting an Array Element when array consists of duplicate values & retain the original value.

3. Deleting an Array Element when array consists of duplicate values & delete the original value as well.

4. Deleting an Array Element when array consists of multiple repeated values using PHP. The original value will be retained, and only the duplicates will be removed.

5. Deleting an Array Element when array consists of multiple repeated values using PHP Remove all duplicated values. The original value will be deleted along with the duplicates and the result will be an array with only unique values.

So let’s get stated with our first case:

Example 1: Remove Element From Array when array consists of unique values using PHP

When to use: Use only when you know a specific item / value and you are sure that it is unique and non-repeated in an array and you want to remove just that specific value.

//Example 1

//Declare an array
$my_states_array = array('Texas', 'Alabama', 'New York', 'Alaska');

//Print the contents of the array.
echo 'Following are the original array contents:<br />';
echo '<pre>';
print_r($my_states_array);
echo '</pre>';

//Remove "Texas" value from array
if( ( $key = array_search('Texas', $my_states_array) ) !== false ) 
{
    unset($my_states_array[$key]);
}

//Print the contents of the array.
echo 'Following are the array contents AFTER removing the value from array:<br />';
echo '<pre>';
print_r($my_states_array);
echo '</pre>';

Example 2: Remove Element From Array when array consists of duplicate values using PHP and retain only the original value

When to use: Use when you know a specific value and you think there might be repeated values of this value and you would want to retain only the original value and delete the rest of the duplicate values.

//Example 2

$my_states_array = array('Texas', 'Alabama', 'New York', 'Alaska', 'Texas', 'Utah');

//Print the contents of the array.
echo 'Following are the original array contents:<br />';
echo '<pre>';
print_r($my_states_array);
echo '</pre>';

//Remove "Texas" value from array, only the ones that are duplicated & retain the original value
$my_states_array = array_unique($my_states_array);

//Print the contents of the array. 
echo 'Following are the array contents AFTER removing the value from array, retaining the original Texas value:<br />';
echo '<pre>';
print_r($my_states_array);
echo '</pre>';

Example 3: Remove Element From Array when array consists of unique values using PHP and delete the original value as well

When to use: Use when you want to delete a specific value or item from an array when you think this array might have repeated values for this item. So this way, the duplicated values will be deleted along with original value.

//Example 3

$my_states_array = array('Texas', 'Alabama', 'New York', 'Alaska', 'Texas', 'Utah');

//Print the contents of the array.
echo 'Following are the original array contents:<br />';
echo '<pre>';
print_r($my_states_array);
echo '</pre>';

//Remove All "Texas" value from array including original value
$my_states_array = array_diff($my_states_array, array('Texas'));

//Print the contents of the array. 
echo 'Following are the array contents AFTER removing the value from array, removing even the original Texas value:<br />';
echo '<pre>';
print_r($my_states_array);
echo '</pre>';

Example 4: Remove Element From Array when array consists of multiple repeated values using PHP and RETAIN the original value

When to use: Use when you have a dynamic array that has multiple repeated values and you don’t know what these values might be. So if you are not worried about what these values are and don’t care about them, but you want to retain only unique values plus only one of the duplicated items in the array and delete the duplicated items, then go ahead & use this.

//Example 4

$my_states_array = array('Texas', 'Alabama', 'New York', 'Alaska', 'Texas', 'Utah',  'Alaska', 'New York');

//Print the contents of the array.
echo 'Following are the original array contents:<br />';
echo '<pre>';
print_r($my_states_array);
echo '</pre>';

//Remove All multiple duplicate values and retain original value
$my_states_array = array_unique($my_states_array);

//Print the contents of the array. 
echo 'Following are the array contents AFTER removing the value from array, removing even the original value:<br />';
echo '<pre>';
print_r($my_states_array);
echo '</pre>';

Example 5: Remove Element From Array when array consists of multiple repeated values using PHP and DELETE the original value

When to use: Use when you have a dynamic array that has multiple repeated values  and you don’t know what these values might be. So if you are not worried about what these values are and don’t care about them, but you want to retain purely only unique values in the array and delete all the duplicated items, then go ahead & use this. So if an array item is repeated at least once, then it will be removed from the result, thereby giving you only pure, non-repeating values.

//Example 5

$my_states_array = array('Texas', 'Alabama', 'New York', 'Alaska', 'Texas', 'Utah',  'Alaska', 'New York');

//Print the contents of the array.
echo 'Following are the original array contents:<br />';
echo '<pre>';
print_r($my_states_array);
echo '</pre>';

//First get all unique values of array
$unique_array = array_unique($my_states_array);

echo 'Following are the array contents AFTER applying array_unique function. We get only the unique values and this will leave out multiple, repeated values<br />';
echo '<pre>';
print_r($unique_array);
echo '</pre>';

//Compute the difference of the original array and the unique array and return an array containing all the values from $my_states_array that are not present in the other arrays ($unique_array). So this means that only the unique, non-repeated values will be left out and the resultant array will contain values that were repeated at least once.
$compared_array = array_diff_assoc($my_states_array, $unique_array);

//Print the contents of the compared_array. 
echo 'Following are the array contents AFTER applying array_diff_assoc. This will leave the most unique, non-repeated values and will return an array of values that were repeated at least once:<br />';
echo '<pre>';
print_r($compared_array);
echo '</pre>';

//Apply array_diff to compute difference of two arrays and return the difference i.e  it returns an array containing all the entries from my_states_array that are not present in any of the other arrays i.e. $compared_array.
$all_unique_non_repeated_values = array_diff($my_states_array, $compared_array);

echo 'Following are the array contents AFTER applying array_diff. This will leave the most unique, non-repeated values and will return an array of values that were repeated at least once. So the following is our FINAL DESIRED RESULT:<br />';
echo '<pre>';
print_r($all_unique_non_repeated_values);
echo '</pre>';
echo '<br />/*=============================================================================*/<br />';

That’s it!
Do you know of any other ways to use PHP to delete item from array? Feel free to share by commenting below.

Share your thoughts, comment below now!

*

*