PHP: Delete an element from an array

Ben Source

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.

phparrays

Answers

answered 10 years ago Konrad Rudolph #1

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

Delete one array element

If you want to delete just one array element you can use unset() or alternative array_splice().

Also if you have the value and don't know the key to delete the element you can use array_search() to get the key.

unset() method

Note that when you use unset() the array keys won't change/reindex. If you want to reindex the keys you can use array_values() after unset() which will convert all keys to numerical enumerated keys starting from 0.

Code

<?php

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

?>

Output

Array (
    [0] => a
    [2] => c
)

array_splice() method

If you use array_splice() the keys will be automatically reindexed, but the associative keys won't change opposed to array_values() which will convert all keys to numerical keys.

Also array_splice() needs the offset, not the key!, as second parameter.

Code

<?php

    $array = array(0 => "a", 1 => "b", 2 => "c");
    array_splice($array, 1, 1);
                       //↑ Offset which you want to delete

?>

Output

Array (
    [0] => a
    [1] => c
)

array_splice() same as unset() take the array by reference, this means you don't want to assign the return values of those functions back to the array.

Delete 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 if you know the values or the keys of the elements which you want to delete.

array_diff() method

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/reindex the keys of the array.

Code

<?php

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

?>

Output

Array (
    [1] => b
)

array_diff_key() method

If you know the keys of the elements which you want to delete, then you want to use array_diff_key(). Here you have to make sure you pass the keys as keys in the second parameter and not as values. Otherwise you have to flip the array with array_flip(). And also here the keys won't change/reindex.

Code

<?php

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

Output

Array (
    [1] => b
)

Also 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.

answered 10 years ago Eran Galperin #2

unset($array[$index]);

answered 10 years ago Stefan Gehrig #3

It should be noted that unset() will keep indexes untouched, which is what you'd expect when using string indexes (array as hashtable), but can be quite surprising when dealing with integer indexed arrays:

$array = array(0, 1, 2, 3);
unset($array[2]);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [3]=>
  int(3)
} */

$array = array(0, 1, 2, 3);
array_splice($array, 2, 1);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(3)
} */

So array_splice() can be used if you'd like to normalize your integer keys. Another option is using array_values() after unset():

$array = array(0, 1, 2, 3);

unset($array[2]);
$array = array_values($array);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(3)
} */

answered 8 years ago DefenestrationDay #4

Also, for a named element:

unset($array["elementName"]);

answered 7 years ago Marcel Cozma #5

  // our initial array  
   $arr = array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red");  
  print_r($arr);

  // remove the elements who's values are yellow or red  
   $arr = array_diff($arr, array("yellow", "red"));
  print_r($arr);  

This is the output from the code above:

Array
(
    [0] => blue
    [1] => green
    [2] => red
    [3] => yellow
    [4] => green
    [5] => orange
    [6] => yellow
    [7] => indigo
    [8] => red
)

Array
(
    [0] => blue
    [1] => green
    [4] => green
    [5] => orange
    [7] => indigo
)

Now, array_values() will reindex a numerical array nicely, but will remove all key strings from the array and replace them with numbers. If you need to preserve the key names (strings), or reindex the array if all keys are numerical, use array_merge():

$arr = array_merge(array_diff($arr, array("yellow", "red")));
print_r($arr);

outputs

Array
(
    [0] => blue
    [1] => green
    [2] => green
    [3] => orange
    [4] => indigo
)

answered 7 years ago liamvictor #6

$key = array_search($needle,$array);
if($key!==false){
    unset($array[$key]);
}

answered 6 years ago Robin Nixon #7

If you have a numerically indexed array where all values are unique (or they are non-unique but you wish to remove all instances of a particular value), you can simply use array_diff() to remove a matching element, like this:

$my_array = array_diff($my_array, array('Value_to_remove'));

For example:

$my_array = array('Andy', 'Bertha', 'Charles', 'Diana');
echo sizeof($my_array) . "\n";
$my_array = array_diff($my_array, array('Charles'));
echo sizeof($my_array);

This displays the following:

4
3

In this example, the element with the value 'Charles' is removed as can be verified by the sizeof() calls that report a size of 4 for the initial array, and 3 after the removal.

answered 5 years ago Sunil Kumar Sain #8

$arr = array('orange', 'banana', 'apple', 'raspberry');
$result= array_pop($arr);
print_r($result);

answered 5 years ago Saurabh Chandra Patel #9

<?php
    $stack = array("fruit1", "fruit2", "fruit3", "fruit4");
    $fruit = array_shift($stack);
    print_r($stack);

    echo $fruit;
?>

Output:

Array
(
    [0] => fruit2
    [1] => fruit3
    [2] => fruit4
)

fruit1

answered 5 years ago Ankit Aggarwal #10

unset() destroys the specified variables.

The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

<?php
function destroy_foo() 
{
    global $foo;
    unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo;
?>

The Answer of the above code will be bar

To unset() a global variable inside of a function

<?php
function foo() 
{
    unset($GLOBALS['bar']);
}

$bar = "something";
foo();
?>

answered 5 years ago Oxydel #11

I'd just like to say I had a particular Object, that had variable attributes (it was basically mapping a table and I was changing the columns in the table, so the attributes in the object, reflecting the table would vary as well

class obj {
    protected $fields = array('field1','field2');
    protected $field1 = array();
    protected $field2 = array();
    protected loadfields(){} 
    // This will load the $field1 and $field2 with rows of data for the column they describe
    protected function clearFields($num){
        foreach($fields as $field) {
            unset($this->$field[$num]); 
            // This did not work the line below worked
            unset($this->{$field}[$num]); // You have to resolve $field first using {}
        }
    }
}

The whole purpose of $fields was just so I don't have to look everywhere in the code when they're changed, I just look at the beginning of the class and change the list of attributes and the $fields array content to reflect the new attributes.

Took me a little while to figure this out. Hope this can help someone.

answered 3 years ago Tebe #12

Suppose you have such an array:

Array
(
    [user_id] => 193
    [storage] => 5
)

To delete storage, do:

unset($attributes['storage']);
$attributes = array_filter($attributes);

And you get:

Array
(
    [user_id] => 193
)

answered 3 years ago KTAnj #13

Destroy a single element of an array

unset()

$array1 = array('A', 'B', 'C', 'D', 'E');
unset($array1[2]); // Delete known index(2) value from array
var_dump($array1);

The output will be:

array(4) {
  [0]=>
  string(1) "A"
  [1]=>
  string(1) "B"
  [3]=>
  string(1) "D"
  [4]=>
  string(1) "E"
}

If you need to re index the array:

$array1 = array_values($array1);
var_dump($array1);

Then the output will be:

array(4) {
  [0]=>
  string(1) "A"
  [1]=>
  string(1) "B"
  [2]=>
  string(1) "D"
  [3]=>
  string(1) "E"
}

Pop the element off the end of array - return the value of the removed element

mixed array_pop(array &$array)

$stack = array("orange", "banana", "apple", "raspberry");
$last_fruit = array_pop($stack);
print_r($stack);
print_r('Last Fruit:'.$last_fruit); // Last element of the array

The output will be

Array
(
    [0] => orange
    [1] => banana
    [2] => apple
)
Last Fruit: raspberry

Remove the first element (red) from an array, - return the value of the removed element

mixed array_shift ( array &$array )

$color = array("a" => "red", "b" => "green" , "c" => "blue");
$first_color = array_shift($color);
print_r ($color);
print_r ('First Color: '.$first_color);

The output will be:

Array
(
    [b] => green
    [c] => blue
)
First Color: red

answered 2 years ago msvairam #14

Follow default functions

i)

$Array = array("test1","test2","test3","test3");

unset($Array[2]);

ii)

$Array = array("test1","test2","test3","test3");

array_pop($Array);

iii)

$Array = array("test1","test2","test3","test3");

array_splice($Array,1,2);

iv)

$Array = array("test1","test2","test3","test3");

array_shift($Array);

answered 2 years ago spyle #15

If you have to delete multiple values in an array and the entries in that array are objects or structured data, [array_filter][1] is your best bet. Those entries that return a true from the callback function will be retained.

$array = [
    ['x'=>1,'y'=>2,'z'=>3], 
    ['x'=>2,'y'=>4,'z'=>6], 
    ['x'=>3,'y'=>6,'z'=>9]
];

$results = array_filter($array, function($value) {
    return $value['x'] > 2; 
}); //=> [['x'=>3,'y'=>6,z=>'9']]

answered 2 years ago John Slegers #16

Associative arrays

For associative arrays, use unset :

$arr = array('a' => 1, 'b' => 2, 'c' => 3);
unset($arr['b']);

// RESULT : array('a' => 1, 'c' => 3)

Numeric arrays

For numeric arrays, use array_splice :

$arr = array(1, 2, 3);
array_splice($arr, 1, 1);

// RESULT : array(0 => 1, 1 => 3)

Note

Using unset for numeric arrays will not produce an error, but it will mess up your indexes :

$arr = array(1, 2, 3);
unset($arr[1]);

// RESULT : array(0 => 1, 2 => 3)

answered 2 years ago GigolNet Guigolachvili #17

/*
 * Remove by value
 */
public function removeFromArr($arr, $val)
{
    unset($arr[array_search($val, $arr)]);
    return array_values($arr);
}

answered 2 years ago Simon #18

If you need to remove multiple elements from an associative array, you can use array_diff_key() (here used with array_flip()):

$my_array = array(
  "key1" => "value 1",
  "key2" => "value 2",
  "key3" => "value 3",
  "key4" => "value 4",
  "key5" => "value 5",
);

$to_remove = array("key2", "key4");

$result = array_diff_key($my_array, array_flip($to_remove));

print_r($result);

Output:

Array ( [key1] => value 1 [key3] => value 3 [key5] => value 5 ) 

answered 2 years ago UMAR FAROOQUE KHAN #19

<?php 
//If you want to remove a particular array element use this method
$my_array = array("key1"=>"value 1","key2"=>"value 2","key3"=>"value 3");

print_r($my_array);
if(array_key_exists("key1",$my_array)){  
unset($my_array['key1']);
print_r($my_array);
}else{
echo "Key does not exist";
}
?>

<?php 
//To remove first array element
$my_array = array("key1"=>"value 1","key2"=>"value 2","key3"=>"value 3");
print_r($my_array);
$new_array=array_slice($my_array,1); 
print_r($new_array);
?>


<?php 
echo "<br/> ";
//To remove first array element to length
//starts from first and remove two element 
$my_array = array("key1"=>"value 1","key2"=>"value 2","key3"=>"value 3");
print_r($my_array);
$new_array=array_slice($my_array,1,2); 
print_r($new_array);
?>

Output

 Array ( [key1] => value 1 [key2] => value 2 [key3] => 
 value 3 ) Array (    [key2] => value 2 [key3] => value 3 ) 
 Array ( [key1] => value 1 [key2] => value 2 [key3] => value 3 ) 
 Array ( [key2] => value 2 [key3] => value 3 )
 Array ( [key1] => value 1 [key2] => value 2 [key3] => value 3 ) 
 Array ( [key2] => value 2 [key3] => value 3 ) 

answered 2 years ago Klajdi Dosti #20

This may help...

<?php
    $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    $a2=array("a"=>"purple","b"=>"orange");
    array_splice($a1,0,2,$a2);
    print_r($a1);
    ?>

result will be:

Array ( [0] => purple [1] => orange [c] => blue [d] => yellow )

answered 2 years ago Mugoma J. Okomba #21

To avoid doing a search one can play around with array_diff:

$array = array(3, 9, 11, 20);
$array = array_diff($array, array(11) ); // removes 11

In this case one doesn't have to search/use the key.

answered 1 year ago SparK #22

For those of you who are looking for ruby's hash#delete equivalent in php:

<?php
function array_delete(&$array, $key) {
    if (!isset($array[$key])) {
        return null;
    }

    $value = $array[$key];
    unset($array[$key]);
    return $value;
}

Adding because this was how I got here the first time...

This will not only delete the element from your array but will also return the value stored in that key so you can consume your array in a non-linear fashion.

answered 9 months ago Arun Vitto #23

When deleting an element from an array in PHP, a good function to use is the unset function. Here is an example of its usage:

$anArray = array("X", "Y", "Z");

unset($anArray[0]);

//'dumps' the content of $anArray to the page:
var_dump($anArray);  

The output of the var_dump function will be:

array(2) { [1]=> string(1) "Y" [2]=> string(1) "Z" }

answered 9 months ago MahdiY #24

Remove an array element based on key:

Use unset function like below:

$a = array(
    'salam',
    '10',
    1
);

unset( $a[1] );

print_r( $a );

/*

Output:

Array
(
    [0] => salam
    [2] => 1
)

*/

Remove an array element based on value:

Use array_search function to get element key and use above manner to remove array element like below:

$a = array(
    'salam',
    '10',
    1
);

$key = array_search( 10, $a );

if( $key !== false ) {
    unset( $a[ $key ] );
}

print_r( $a );

/*

Output:

Array
(
    [0] => salam
    [2] => 1
)

*/

answered 8 months ago Drake Boein #25

You can simply use unset() to delete an array.

Remember that array must be unset after foreach function.

answered 7 months ago Joshua Charles Pickwell #26

<?php
$array = array("your array");
$array = array_diff($array, ["element you want to delete"]);
?>

Create your array in the variable $array and then where I have put 'element you want to delete' you put something like: "a". And if you want to delete multiple then: "a", "b".

answered 7 months ago Kristoffer Bohmann #27

unset() multiple, fragmented elements from an array

While unset() has been mentioned here several times, it has yet to be mentioned that unset() accepts multiple variables making it easy to delete multiple, noncontiguous elements from an array in one operation:

// Delete multiple, noncontiguous elements from an array
$array = [ 'foo', 'bar', 'baz', 'quz' ];
unset( $array[2], $array[3] );
print_r($array);
// Output: [ 'foo', 'bar' ]

unset() dynamically

unset() does not accept an array of keys to remove, so the code below will fail (it would have made it slightly easier to use unset() dynamically though).

$array = range(0,5);
$remove = [1,2];
$array = unset( $remove ); // FAILS: "unexpected 'unset'"
print_r($array);

Instead, unset() can be used dynamically in a foreach loop:

$array = range(0,5);
$remove = [1,2];
foreach ($remove as $k=>$v) {
    unset($array[$v]);
}
print_r($array);
// Output: [ 0, 3, 4, 5 ]

Remove array keys by copying the array

There is also another practice that has yet to be mentioned. Sometimes, the simplest way to get rid of certain array keys is to simply copy $array1 into $array2.

$array1 = range(1,10);
foreach ($array1 as $v) {
    // Remove all even integers from the array
    if( $v % 2 ) {
        $array2[] = $v;
    }
}
print_r($array2);
// Output: [ 1, 3, 5, 7, 9 ];

Obviously, the same practice applies to text strings:

$array1 = [ 'foo', '_bar', 'baz' ];
foreach ($array1 as $v) {
    // Remove all strings beginning with underscore
    if( strpos($v,'_')===false ) {
        $array2[] = $v;
    }
}
print_r($array2);
// Output: [ 'foo', 'baz' ]

answered 7 months ago Ziya Vakhobov #28

Here simpler way of deleting with foreach and unset.

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

Result: Array ( [0] => 1 [1] => 2 [2] => 7 [3] => 5 [4] => 6 ) Element 4 is removed.

answered 5 months ago Star #29

Solutions:

  1. To delete one element, use unset( ):
unset($array[3]);
unset($array['foo']);
  1. To delete multiple noncontiguous elements, also use unset( ):
unset($array[3], $array[5]);
unset($array['foo'], $array['bar']);
  1. To delete multiple contiguous elements, use array_splice( ):
array_splice($array, $offset, $length);

Further explanation:

Using these functions removes all references to these elements from PHP. If you want to keep a key in the array, but with an empty value, assign the empty string to the element:

$array[3] = $array['foo'] = '';

Besides syntax, there's a logical difference between using unset( ) and assigning '' to the element. The first says This doesn't exist anymore, while the second says This still exists, but its value is the empty string.

If you're dealing with numbers, assigning 0 may be a better alternative. So, if a company stopped production of the model XL1000 sprocket, it would update its inventory with:

unset($products['XL1000']);

However, if it temporarily ran out of XL1000 sprockets, but was planning to receive a new shipment from the plant later this week, this is better:

$products['XL1000'] = 0;

If you unset( ) an element, PHP adjusts the array so that looping still works correctly. It doesn't compact the array to fill in the missing holes. This is what we mean when we say that all arrays are associative, even when they appear to be numeric. Here's an example:

// create a "numeric" array
$animals = array('ant', 'bee', 'cat', 'dog', 'elk', 'fox');
print $animals[1];  // prints 'bee'
print $animals[2];  // prints 'cat'
count($animals);    // returns 6

// unset( )
unset($animals[1]); // removes element $animals[1] = 'bee'
print $animals[1];  // prints '' and throws an E_NOTICE error
print $animals[2];  // still prints 'cat'
count($animals);    // returns 5, even though $array[5] is 'fox'

// add new element
$animals[ ] = 'gnu'; // add new element (not Unix)
print $animals[1];  // prints '', still empty
print $animals[6];  // prints 'gnu', this is where 'gnu' ended up
count($animals);    // returns 6 

// assign ''
$animals[2] = '';   // zero out value
print $animals[2];  // prints ''
count($animals);    // returns 6, count does not decrease

To compact the array into a densely filled numeric array, use array_values( ):

$animals = array_values($animals);

Alternatively, array_splice( ) automatically reindexes arrays to avoid leaving holes:

// create a "numeric" array
    $animals = array('ant', 'bee', 'cat', 'dog', 'elk', 'fox');
    array_splice($animals, 2, 2);
    print_r($animals);
    Array
    (
        [0] => ant
        [1] => bee
        [2] => elk
        [3] => fox
    )

This is useful if you're using the array as a queue and want to remove items from the queue while still allowing random access. To safely remove the first or last element from an array, use array_shift( ) and array_pop( ), respectively.

answered 3 months ago inrsaurabh #30

unset don't change the index but array_splice does

$arrayName = array( '1' => 'somevalue',
                        '2' => 'somevalue1',
                        '3' => 'somevalue3',
                        500 => 'somevalue500',
                             );


    echo $arrayName['500']; 
    //somevalue500
    array_splice($arrayName, 1,2);

    print_r( $arrayName );
    //Array ( [0] => somevalue [1] => somevalue500 )



    $arrayName = array( '1' => 'somevalue',
                        '2' => 'somevalue1',
                        '3' => 'somevalue3',
                        500 => 'somevalue500',
                             );


    echo $arrayName['500']; 
    //somevalue500
    unset($arrayName[1]);

    print_r( $arrayName );
    //Array ( [0] => somevalue [1] => somevalue500 )

answered 3 months ago Wakar Ahmad Khan #31

$x = array(1, 2, 3, 4, 5);
var_dump($x);
unset($x[3]); // here is key to be delete
echo '<br>';
array_values($x);
var_dump($x);

answered 3 months ago Premjith #32

You can use array_pop method to remove the last element of an array

<?php
$a=array("one","two","Three");
array_pop($a);
print_r($a);
?>
The out put will be 
Array ( [0] => one[1] => two)

answered 2 months ago Mohammad Wasim #33

There are two methods to do this task

unset() and array_splice()

let us assume two arrays

$array_1 =array('a'=>'One','b'=>'Two', 'c'=>'Three');

$array_2 =array('Red','Yellow','White','Black', 'Green');

with unset()

syntax - unset(array_element)

unset($array_1['a']); // any valid key
unset($array_2[0]); // any valid index      
  • after removing array element array index do not change

with array_splice()

syntax - array_splice(array, index, length)

array_splice($array_1,1,1); // remove 1 element from $array_1 from index 1
array_splice($array_2,3,1); // remove 1 element from $array_2 from index 3
  • all array elements are re indexed after removing element from array

answered 2 months ago Akshay Prakash #34

For associative arrays, with non integer keys:

Simply, unset($array[$key]) would work.

For arrays having integer keys and if you want to maintain your keys:

  1. $array = [ 'mango', 'red', 'orange', 'grapes'];

unset($array[2]); $array = array_values($array);

  1. array_splice($array, 2, 1);

comments powered by Disqus