PHP deleting elements of an array by unset ( key or value )

Remove an array element with unset()

unset() removes an array element by key without reindexing the remaining numeric keys. Use array_values() afterwards only when you explicitly need a new zero-based index sequence.

$items = ['red', 'green', 'blue', 'yellow'];
unset($items[1]);
print_r($items);

$reindexed = array_values($items);
print_r($reindexed);

When removing by value, first find the key with array_search() and test the result with !== false before calling unset().

$input=array('a','b','c','d','e','f','g');
// Remove 4th element ( 0 indexed ) 
unset($input[3]); 
foreach ($input as $key => $val) {
echo "$key -> $val <br>";
}
As we are deleting the third element (4th on 0 index) the output of above code is here.
0 -> a 
1 -> b
2 -> c
4 -> e
5 -> f
6 -> g
Array Delete We can remove an element from an array by using unset command.
This unset command takes the array key as input and remove that element from the array. After removal the associated keys and values ( of other balance elements ) does not change.



Remove any element of PHP array by using unset, array_diff() or by searching for value & using key

unset($input[3]);
Array Unset Here we are removing the element with key=3. If the array has 7 elements and if try to delete 9th element then unset command will not return any error but nothing will be deleted.

Here is an example how unset command is used.

How to delete an element by using value ( not key ) from an array

In the above examples we have used key if the array as input to remove the element from the array. If we don't know the key and we know the value then how to remove the element?

There is no direct function to do this but we can use array_diff function to remove the element by using the value. Note that array_diff function takes two arrays as input. Here is the code , we want to remove d from our $input array.
$new_array=array_diff($input,array("d"));
We can also use like this to remove more elements by using its value.
$remove=array('c','f');
$new_array=array_diff($input,$remove);
Display & check our elements after removal like this.
foreach ($new_array as $key => $val) {
echo "$key -> $val <br>";}
The output of the above command is here
0 -> a 
1 -> b
3 -> d
4 -> e
6 -> g
You can read how unset a variable here.

Using array_search()

We can use array_search() to get the key of matching search and then delete by using unset()
$input=array('a','b','c','d','e','f','g');

$key = array_search('d', $input, true);
if ($key !== false) {
    unset($input[$key]);
}

foreach ($input as $key => $val) {
echo "$key -> $val <br>";
}
Output is here
0 -> a
1 -> b
2 -> c
4 -> e
5 -> f
6 -> g

Example 2: Unsetting Multiple Elements Conditionally

Sometimes, you may want to remove elements from an array based on a condition:

$arr = array(10, 20, 30, 40, 50);
foreach ($arr as $key => $value) {
    if ($value % 20 == 0) {
        unset($arr[$key]);
    }
}
print_r($arr); // Output: Array ( [0] => 10 [2] => 30 [4] => 50 )

Example 3: Unsetting the Entire Array

You can completely remove the array using *unset()*:

$arr = array(1, 2, 3);
unset($arr);
echo isset($arr) ? "Array exists" : "Array is unset"; // Output: Array is unset

Example 4: Unsetting a Key in an Associative Array

$arr = array("name" => "John", "age" => 30, "city" => "NY");
unset($arr['age']);
print_r($arr); // Output: Array ( [name] => John [city] => NY )


Questions



Joining Two Arrays by array_merge
Array REFERENCE


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer