Search code snippets, questions, articles...

Delete item from array PHP

    $subjects = ["Math", "Physics", "Chemistry"];
    unset($array[1]); //Remove Single Item(Physics) from array

    array_splice($subjects, 1, 2); //Remove multiple items form array

To delete one or more items from an array in PHP you can use unset() or array_splice() inbuilt methods.

unset(): This PHP method can delete a single record from an array. This takes an array with its value index that needs to be deleted.

array_splice(): If you want to delete multiple items from an array and you know the start index and end index of items that you want to delete, you can use this method. This takes $array as its first required parameter and $start_index as the second parameter and $end_index as its third parameter.

Search Index Data (The code snippet can also be found with below search text)

unset() and array_splice() functions PHP
Was this helpful?
Programming Feeds
Learn something new everyday on Devsheet