How to delete an item from an array in PHP

To delete an item from an array in PHP you can use “unset()”

Here is an example:

$array = [1,2,3,4,5];
              
foreach( $array as $key=>$value ){
    if($value == 3){
        unset($array[$key]);
    }
}

print_r( $array);

Output:

Array
(
[0] => 1
[1] => 2
[3] => 4
[4] => 5
)

Leave a Reply

Your email address will not be published. Required fields are marked *