Deleting an element from an array in PHP

0 votes
Is there a simple PHP method for removing a certain member from an array so that foreach ($array) no longer includes it?

Setting it to null seemed like it might solve the problem, however, it does not appear to.
Jul 25, 2022 in PHP by Kithuzzz
• 38,000 points
637 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Deleting a single array element

Use unset() or, alternatively, array splice to remove just one element from an array ().

You can use array search() to find the key if you know the value but not the key to delete the element. Given that array search only gives the first hit, this only functions if the element does not appear more than once.

unset()
Keep in mind that the keys of the array won't change if you call unset(). Use array values() following unset() to reindex the keys. This will change all of the keys to numerically enumerated keys beginning with 0.

Code:

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

Output:

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

Deleting multiple array elements

Depending on whether you know the values or the keys of the items you wish to delete, you can use the functions array diff() or array diff key() if you want to delete many array members without repeatedly calling unset() or array splice().

array diff() technique
Use "array diff" if you know the values of the array members you want to remove (). The keys of the array won't change, just like with unset() before it.

Code:

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

Output:

[
    [1] => b
]

I hope this helps you.

answered Jul 26, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
1 answer

How to remove duplicate values from an array in PHP?

Hello @kartik, Use array_unique(): Example: $array = array(1, 2, 2, 3); $array ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,840 points
2,848 views
0 votes
1 answer

How to delete element by value in array (not key) using php?

Hello @kartik, Using array_search() and unset, try the following: if (($key = ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,840 points
2,217 views
0 votes
1 answer

How can I get the classname from a static call in an extended PHP class?

Hello @kartik, __CLASS__ always returns the name of the ...READ MORE

answered Oct 27, 2020 in PHP by Niroj
• 82,840 points
1,164 views
0 votes
1 answer

How to pass an array via $_GET in php?

Hii, You can pass an associative array to http_build_query() and ...READ MORE

answered Nov 22, 2020 in PHP by Niroj
• 82,840 points
4,817 views
0 votes
0 answers

How to convert an array to a string in PHP?

What can I do to get the ...READ MORE

May 30, 2022 in PHP by Kichu
• 19,040 points
704 views
0 votes
0 answers

How to convert a string to an array in php

I want to convert a string into ...READ MORE

Jun 1, 2022 in PHP by Kichu
• 19,040 points
723 views
0 votes
0 answers

PHP Unset Array value effect on other indexes

I want to know how unset affects ...READ MORE

Jun 25, 2022 in PHP by narikkadan
• 63,600 points
579 views
0 votes
0 answers

Deleting an element from an array in PHP

Is there a method to delete an ...READ MORE

Aug 12, 2022 in Others by krishna
• 2,820 points
671 views
0 votes
1 answer

How to check if array is multidimensional or not?

Since the 'second dimension' could be just ...READ MORE

answered Nov 5, 2018 in Others by DataKing99
• 8,250 points
6,293 views
0 votes
1 answer

How to store input value into array then localstorage?

Hello @ abhittac, You have create the array everytime the ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
9,342 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP