To remove a specific item from a set in PHP, you can use the unset()
function.
In this example,
$numberSet
with values $numberSet = [1, 2, 3, 4, 5];
.unset()
function to remove the item '2' from $numberSet
.unset()
function removes the specified item from the set.<?php
$numberSet = [1, 2, 3, 4, 5];
unset($numberSet[array_search(2, $numberSet)]);
print_r($numberSet);
?>
Array ( [0] => 1 [1] => 3 [2] => 4 [3] => 5 )
In this example,
$stringSet
with values $stringSet = ['apple', 'banana', 'orange'];
.unset()
function to remove the string 'banana' from $stringSet
.unset()
function removes the specified item from the set.<?php
$stringSet = ['apple', 'banana', 'orange'];
unset($stringSet[array_search('banana', $stringSet)]);
print_r($stringSet);
?>
Array ( [0] => apple [1] => orange )
In this tutorial, we learned How to Remove Specific Item from a Set in PHP language with well detailed examples.