Home >Backend Development >PHP Tutorial >PHP delete array elements and delete duplicate array functions_PHP tutorial
PHP deletes array elements and deletes duplicate array functions. This article mainly talks about deleting array values in PHP. It tells you how to delete elements at a specified position in an array. The second part tells you how to use the array_keys function.
php tutorial to delete array elements and delete duplicate array functions
This article mainly talks about deleting PHP array values. It tells you how to delete elements at a specified position in an array. The second article tells you how to use the array_keys function to delete duplicate elements of an array.
*/
$a=array("red", "green", "blue", "yellow");
count($a); //get 4
unset($a[1]); //Delete the second element
count($a); //get 3
echo $a[2]; //There are only three elements in the array. I wanted to get the last element, but I got blue,
echo $a[1]; //No value//array array_splice (array input, int offset [, int length [, array replacement]])
//array_splice() is actually a function that replaces array elements, but if you do not add a replacement value, it simply deletes the element. The following is the usage of array_splice():
$b=array("red", "green", "blue", "yellow");
array_splice($a,1,1);
//Let’s look at a more comprehensive method of deleting duplicate values and deleting specified array elements
$array1 = array(1 => "www.bkjia.com", 2 => "Pineapple", 4 => " => "Banana",4 => "Guava",5 => " => "www.bkjia.com");
$search_keys = array_keys($array1, "www.bkjia.com");
foreach($search_keys as $key) {
unset($array1[$key]);
}
print_r($array1);
/*
Get results
array ( [2] => Pineapple [4] => Guava [3] => Banana )
*/
//Function to delete duplicate elements in an array
function delmember(&$array, $id)
{
$size = count($array);
for($i = 0; $i <$size - $id - 1; $i ++)
{
$array[$id + $i] = $array[$id + $i + 1];
}
unset($array[$size - 1]);
}
?>