Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 88817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:37:10+00:00 2026-05-10T22:37:10+00:00

Is there an easy way to delete an element from an array using PHP,

  • 0

Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element?

I thought that setting it to null would do it, but apparently it does not work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:37:10+00:00Added an answer on May 10, 2026 at 10:37 pm

    There are different ways to delete an array element, where some are more useful for some specific tasks than others.

    Deleting a Single Array Element

    If you want to delete just one single array element you can use unset() and alternatively array_splice().

    By key or by value?

    If you know the value and don’t know the key to delete the element you can use array_search() to get the key. This only works if the element doesn’t occur more than once, since array_search() returns the first hit only.

    unset() Expression

    Note: When you use unset() the array keys won’t change. If you want to reindex the keys you can use array_values() after unset(), which will convert all keys to numerically enumerated keys starting from 0 (the array remains a list).

    Example Code:

    $array = [0 => "a", 1 => "b", 2 => "c"]; unset($array[1]);           // ↑ Key of element to delete 

    Example Output:

    [     [0] => a     [2] => c ] 

    array_splice() Function

    If you use array_splice() the (integer) keys will automatically be reindex-ed, but the associative (string) keys won’t change — as opposed to array_values() after unset(), which will convert all keys to numerical keys.

    Note: array_splice() needs the offset, not the key, as the second parameter; offset = array_flip(array_keys(array))[key].

    Example Code:

    $array = [0 => "a", 1 => "b", 2 => "c"]; array_splice($array, 1, 1);                   // ↑ Offset of element to delete 

    Example Output:

    [     [0] => a     [1] => c ] 

    array_splice(), same as unset(), take the array by reference. You don’t assign the return values back to the array.

    Deleting Multiple Array Elements

    If you want to delete multiple array elements and don’t want to call unset() or array_splice() multiple times you can use the functions array_diff() or array_diff_key() depending on whether you know the values or the keys of the elements to remove from the array.

    array_diff() Function

    If you know the values of the array elements which you want to delete, then you can use array_diff(). As before with unset() it won’t change the keys of the array.

    Example Code:

    $array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"]; $array = array_diff($array, ["a", "c"]);                          // └────────┘                          // Array values to delete 

    Example Output:

    [     [1] => b ] 

    array_diff_key() Function

    If you know the keys of the elements which you want to delete, then you want to use array_diff_key(). You have to make sure you pass the keys as keys in the second parameter and not as values. Keys won’t reindex.

    Example Code:

    $array = [0 => "a", 1 => "b", 2 => "c"]; $array = array_diff_key($array, [0 => "xy", "2" => "xy"]);                               // ↑           ↑                               // Array keys of elements to delete 

    Example Output:

    [     [1] => b ] 

    If you want to use unset() or array_splice() to delete multiple elements with the same value you can use array_keys() to get all the keys for a specific value and then delete all elements.

    array_filter() Function

    If you want to delete all elements with a specific value in the array you can use array_filter().

    Example Code:

    $array = [0 => "a", 1 => "b", 2 => "c"]; $array = array_filter($array, static function ($element) {     return $element !== "b";     //                   ↑     // Array value which you want to delete }); 

    Example Output:

    [     [0] => a     [2] => c ] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 171k
  • Answers 171k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer it'matter of *unix permissions, gain root acces, for example by… May 12, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer jQuery would allow for a succinct solution, whilst hiding most… May 12, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer If your site database contains it's own structure for membership,… May 12, 2026 at 2:16 pm

Related Questions

I looked over this web and google and the solutions didn't work for me.
I know di< will delete in an HTML tag itself. Is there an easy
Possible Duplicate: Emacs: how to delete text without kill ring? I am sick of
Possible Duplicate: How do you remove Subversion control for a folder? Command line to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.