• 2
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 190

Backtrace:

File: /var/www/stackquestions.com/public_html/application/views/question.php
Line: 190
Function: _error_handler

File: /var/www/stackquestions.com/public_html/application/controllers/Questions.php
Line: 416
Function: view

File: /var/www/stackquestions.com/public_html/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to delete an element from an array in PHP

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.

// 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 it 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
)
  • 0
Reply Report

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)
} */
  • 1
Reply Report

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 alternatively 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 = [0 => "a", 1 => "b", 2 => "c"];
    unset($array[1]);
                //↑ Key which you want to delete

?>

Output

[
    [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 as opposed to array_values() which will convert all keys to numerical keys.

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

Code

<?php

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

?>

Output

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

array_splice() same as unset() take the array by reference, and 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 = [0 => "a", 1 => "b", 2 => "c"];
    $array = array_diff($array, ["a", "c"]);
                               //└────────┘→ Array values which you want to delete

?>

Output

[
    [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 = [0 => "a", 1 => "b", 2 => "c"];
    $array = rray_diff_key($array, [0 => "xy", "2" => "xy"]);
                                    //↑           ↑ Array keys which you want to delete
?>

Output

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

  • 1
Reply Report