Demo For: PHP Delete Item From Array Examples
/*=============================================================================*/Example 1: Delete Item From Array when array consists of unique values using PHP - Remove value "Texas"
/*=============================================================================*/
Following are the original array contents:
Array
(
[0] => Texas
[1] => Alabama
[2] => New York
[3] => Alaska
)
Following are the array contents AFTER removing the value from array:Array
(
[1] => Alabama
[2] => New York
[3] => Alaska
)
/*=============================================================================*/Example 2: Delete Item From Array when array consists of repeated duplicate values using PHP
Remove all duplicated value "Texas". The original "Texas" value will be retained, the rest will be removed.
/*=============================================================================*/
Following are the original array contents:
Array
(
[0] => Texas
[1] => Alabama
[2] => New York
[3] => Alaska
[4] => Texas
[5] => Utah
)
Following are the array contents AFTER removing the value from array, retaining the original Texas value:Array
(
[0] => Texas
[1] => Alabama
[2] => New York
[3] => Alaska
[5] => Utah
)
/*=============================================================================*/Example 3: Delete Item From Array when array consists of repeated values using PHP
Remove all duplicated value "Texas". The original "Texas" value will be retained, the rest will be removed.
/*=============================================================================*/
Following are the original array contents:
Array
(
[0] => Texas
[1] => Alabama
[2] => New York
[3] => Alaska
[4] => Texas
[5] => Utah
)
Following are the array contents AFTER removing the value from array, removing even the original Texas value:Array
(
[1] => Alabama
[2] => New York
[3] => Alaska
[5] => Utah
)
/*=============================================================================*/Example 4: Delete Item From Array when array consists of multiple repeated values using PHP
Remove all duplicated values. The original value will be retained, and only the duplicates will be removed.
/*=============================================================================*/
Following are the original array contents:
Array
(
[0] => Texas
[1] => Alabama
[2] => New York
[3] => Alaska
[4] => Texas
[5] => Utah
[6] => Alaska
[7] => New York
)
Following are the array contents AFTER removing the value from array, removing even the original value:Array
(
[0] => Texas
[1] => Alabama
[2] => New York
[3] => Alaska
[5] => Utah
)
/*=============================================================================*/Example 5: Delete Item From Array when array consists of multiple repeated values using PHP
Remove all duplicated values. The original value will be deleted along with the duplicates.
/*=============================================================================*/
Following are the original array contents:
Array
(
[0] => Texas
[1] => Alabama
[2] => New York
[3] => Alaska
[4] => Texas
[5] => Utah
[6] => Alaska
[7] => New York
)
Following are the array contents AFTER applying array_unique function. We get only the unique values and this will leave out multiple, repeated valuesArray
(
[0] => Texas
[1] => Alabama
[2] => New York
[3] => Alaska
[5] => Utah
)
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:Array
(
[4] => Texas
[6] => Alaska
[7] => New York
)
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:Array
(
[1] => Alabama
[5] => Utah
)
/*=============================================================================*/