Menu

Add Topic

Forum

waiting answer July 28, 2021

How to Delete an Element from an Array in PHP

Answers
June 10, 2021

The PHP built-in function unset() is used to delete an element from an array in PHP. The unset() function in PHP can also use to destroy any other variable. The unset command inputs the array key as input and removed the element from that array.

Delete an element from a single-dimensional array:

     <?php
        $array = array('blue', 'green', 'yellow', 'red', 'violet');
        unset($arr2[1]); //RESULT: array(0 => 'blue', 2 => 'yellow', 3=> 'red', 4=>'violet')
    ?>

Delete an element from a multidimensional array

     <?php
        $array = array("a" => "Apple", "b" => "Ball", "c" => "Cat");
        unset($arr1["a"]);
        // RESULT: array("b" => "Ball", "c" => "Cat")
    ?>

0 0

Please Login to Post the answer

Leave an Answer