# Remove Elements from a PHP Array by Key, Value, or Index

> Use unset(), array_splice(), array_diff(), and array_diff_key() to delete single or multiple elements from PHP arrays by key, value, or index position

**URL:** https://sentry.io/answers/deleting-elements-from-an-array-in-php/

---

## The Problem

How do I remove elements from an array in PHP? 

## The Solution

The easiest way to delete an element from an array in PHP is by using `unset()`.

```php
$fruits = ['apple', 'orange', 'pear'];
unset($fruits[1]);
```

However, note that this does not re-index the array, so `$fruits[2]` will remain 'pear'. We'll go into more detail about that and explore some other options below.

### Deleting a Single Element 

To delete a single element from an array, you can use:

- The `array_splice()` function, which removes the element based on its index and reindexes the array.
- The `unset()` function, which removes the element by its key and removes the element's index.

#### The `array_splice()` Function

The `array_splice()` function is the most versatile and direct solution for deleting an element from both indexed arrays and associative arrays (with key-value pairs).

We call the `array_splice()` function with three arguments:
  - the array,
  - the offset of the element to be removed, and 
  - the number of elements you want to remove. 

Given an indexed array, you can remove the element at index `0` and specify only one element to be removed:

```php
$fruits = ['apple', 'orange', 'pear'];

// remove the first element and only remove one element
array_splice($fruits, 0, 1);
print_r($fruits);
```

This will output:

```
Array
(
    [0] => orange
    [1] => pear
)
```

The `array_splice()` function is a convenient method to delete a single element from an associative array, as you can remove an element based on its offset rather than its key or value. For example:

```php
$codes = [
    'red' => 'apple',
    'orange' => 'orange',
    'blue' => 'blueberry'
];

array_splice($codes, 0, 1);
print_r($codes);
```

Output:

```
Array
(
    [orange] => orange
    [blue] => blueberry
)
```

#### The `unset()` Function

Use `unset()` to remove an element by its key. 

```php
$fruits = ['apple', 'orange', 'pear'];

// remove the second element
unset($fruits[1]);
print_r($fruits);
```

The output is:

```
Array
(
    [0] => apple
    [2] => pear
)
```

Removing an element from an array using the `unset()` function results in the element's index being removed too. If the consecutive numerical indexing of your array is important, you can follow the `unset()` function with the `array_values()` function. The `array_values()` function doesn't update the array but returns a new array, so we need to create a variable to store the value of `array_values()`.

Here we reindex the array with `array_values()` so that `pear` is at index one:

```php
// array_values() - converts keys to numerical values
$reset = array_values($fruits);
print_r($reset);
```

The output is:

```
Array
(
    [0] => apple
    [1] => pear
)
````

Note that using the `array_values()` function with an associative array will return a new array with numeric keys, for example:

```php
$colors = [
    'red' => 'apple',
    'orange' => 'orange',
    'blue' => 'blueberry'
];

$colorsTest = array_values($colors);

print_r($colorsTest);
```

The output is:

```
Array
(
    [0] => apple
    [1] => orange
    [2] => blueberry
)
```

If you want to delete an element from an array but you only know its value, you can use `array_search()` to find the key of the element, and then use `unset()` to remove the key-value pair. Note that if there are duplicate elements in the array, `array_search()` will only return the first match.

```php
$colors = [
    'red' => 'apple',
    'orange' => 'orange',
    'blue' => 'blueberry'
];

$findKey = array_search('apple', $colors);
print_r($findKey);
```

The output is:

```
red
```

Now we use `unset()` to remove the key-value pair from the array:

```php
unset($colors['red']);
print_r($colors);
```

Output:

```
Array
(
    [orange] => orange
    [blue] => blueberry
)
```

### Deleting Multiple Elements

To delete multiple nonconsecutive elements from an array, you can use:

- The `array_diff()` function, which deletes elements and their indices from an indexed or associative array with the values as input.
- The `array_diff_key()` function, which removes elements from an associative array using their keys as arguments.

#### The `array_diff()` Function

In this example, we call `array_diff()` on an indexed array to create a new array without `John`, `Sue`, and `Sean`:

```php
$names = ['John', 'Mary', 'Sue', 'Daniel', 'Sean'];
$newNames = array_diff($names, ['John', 'Sue', 'Sean']);

print_r($newNames);
```

The output contains the remaining elements but the indexes are no longer sequential:

```
Array
(
    [1] => Mary
    [3] => Daniel
)
```

As with `unset()`, you can use `array_values()` to reindex the output:

```
Array
(
    [0] => Mary
    [1] => Daniel
)
```

We can use `array_diff()` on an associative array to create a new array with elements removed based on their values. This is handy in cases where you aren't sure of the keys of the elements to be removed.

```php
$jobs = [
    'Lawyer' => 'John',
    'Teacher' => 'Mary',
    'Chef' => 'Sue',
    'Driver' => 'Daniel',
    'Doctor' => 'Sean'
];

$newJobs = array_diff($jobs, ['John', 'Sue', 'Sean']);
print_r($newJobs);
```

Output:

```
Array
(
    [Teacher] => Mary
    [Driver] => Daniel
)
```

#### The `array_diff_key()` Function

Use the `array_diff_key()` function when you want to delete several elements from an array using their key values.

```php
$times = [
    '8:00' => 'morning',
    '12:00' => 'noon',
    '19:00' => 'night'
];

// the values can be set to an empty string, or any character or string
$newTimes = array_diff_key($times, ['8:00' => '', '12:00' => '']);
print_r($newTimes);
```

Output:

```
Array
(
    [19:00] => night
)
```

---

*Source: [sentry.io/answers/deleting-elements-from-an-array-in-php/](https://sentry.io/answers/deleting-elements-from-an-array-in-php/)*
