In case you do not know what the first item's key is:

// Make sure to reset the array's current index
reset($array);

array);
unset($array[$key]);
Answer from aefxx on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_array_shift.asp
PHP array_shift() Function
โฎ PHP Array Reference ยท Remove ...een","c"=>"blue"); echo array_shift($a); print_r ($a); ?> Try it Yourself ยป ยท The array_shift() function removes the first element from an array, and returns the value of the removed ...
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.array-shift.php
PHP: array_shift - Manual
<?php function array_last_entry($arr){ if(!is_array($arr)) return; if(empty($arr)) return; return end($arr); } function array_first_entry($arr){ if(!is_array($arr)) return; if(empty($arr)) return; reset($arr); return current($arr); } $arr = array( '5' => 'five', '3' => 'three', '8' => 'eight',); echo 'last entry: '.array_last_entry($arr).'<br>'; echo 'first entry: '.array_first_entry($arr).'<br>'; echo 'alternative output:<br>'; echo 'last entry: '.$arr[count($arr)-1]; echo '<br>first entry: '.$arr[0]; ?> The output will look like: last entry: eight first entry: five alternative output: last entry: first entry: As you can see, if you have to handle arrays with non-continuous indexes, these functions may be very helpful. ... no, it demonstrates quite well that it removes the first element in the original array, updating the keys, and that it also returns the original first element.
People also ask

What is the use of array_shift in PHP?
$fruits = ['apple', 'banana', 'cherry'];
$first = array_shift($fruits);
print_r($fruits); // ['banana', 'cherry']
echo $first; // apple
It removes the first element from the array and returns it.
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php array_shift: remove the first array element with examples
PHP array_shift: Remove the First Array Element with Examples - ...
What is the difference between array_shift and array_pop?
$arr = [1, 2, 3];
array_shift($arr); // removes 1 from start
array_pop($arr); // removes last element
array_shift removes from the beginning, array_pop removes from the end.
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php array_shift: remove the first array element with examples
PHP array_shift: Remove the First Array Element with Examples - ...
How does array_shift work with associative arrays?
$user = ['name' => 'John', 'age' => 25];
$first = array_shift($user);
print_r($user); // ['age' => 25]
echo $first; // John
It removes the first key-value pair based on internal order.
๐ŸŒ
flatcoding.com
flatcoding.com โ€บ home โ€บ php array_shift: remove the first array element with examples
PHP array_shift: Remove the First Array Element with Examples - ...
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_arrays_remove.asp
PHP Remove Array Items
In PHP, you can remove/delete array ... an associative array ยท array_pop() - removes the last array item ยท array_shift() - removes the first array item ยท...
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-remove-the-first-element-from-an-array-in-php.php
How to Remove the First Element from an Array in PHP
You can use the PHP array_shift() function to remove the first element or value from an array. The array_shift() function also returns the removed value of array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-remove-first-element-from-an-array-in-php
How to Remove First Element from an Array in PHP? - GeeksforGeeks
July 23, 2025 - The array_shift() function removes the first element from an array and returns it. This function modifies the original array. ... <?php $arr = array(10, 20, 30, 40, 50, 60); $removedElement = array_shift($arr); echo "Removed element: " . ...
๐ŸŒ
W3 School of Coding
w3schoolofcoding.com โ€บ home โ€บ php โ€บ removing the first element from an array in php
Removing the First Element from an Array in PHP - W3 School of Coding
November 1, 2024 - PHP has the ability to support both associative and indexed arrays, giving developers the flexibility to select the best structure for their data. One of the most straightforward ways to remove the first element from an array in PHP is by using the โ€˜array_shift()โ€™ function.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ remove-first-element-from-an-array-in-php
Remove First Element from an Array in PHP - GeeksforGeeks
July 23, 2025 - The array_slice() function is used to extract a portion of an array. By specifying a start index of 1, you can extract all elements except the first one. Example: This example shows the implementation of the above-explained approach.
๐ŸŒ
FlatCoding
flatcoding.com โ€บ home โ€บ php array_shift: remove the first array element with examples
PHP array_shift: Remove the First Array Element with Examples - FlatCoding
September 7, 2025 - The array_shift is a built-in function used to remove and return the first value from an array. The array changes in place and loses one element from the start. It can handle both indexed arrays and associative arrays.
๐ŸŒ
w3resource
w3resource.com โ€บ php โ€บ function-reference โ€บ array_shift.php
PHP : array_shift() function - w3resource
The array_shift() function is used to remove the first element from an array, and returns the value of the removed element. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.
๐ŸŒ
The Code Developer
thecodedeveloper.com โ€บ home โ€บ php โ€บ how to remove the first element from an array in php
How to Remove the First element from an array in PHP - PHP Tutorial
December 1, 2018 - <?php $stack = array("yellow", "red", "green", "orange", "purple"); // delete the first element of an array $removed = array_shift($stack); print_r($stack); ?>
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-remove-first-element-from-an-array-in-php
How to remove first element from an array in PHP? - javatpoint
We are excited to announce that we are moving from JavaTpoint.com to TpointTech.com on 10th Feb 2025. Stay tuned for an enhanced experience with the same great content and even more features. Thank you for your continued support ยท To remove the first element or value from an array, array_shift() ...
๐ŸŒ
TecAdmin
tecadmin.net โ€บ removing-first-element-from-array-in-php
Removing the First Element from an Array in PHP โ€“ TecAdmin
April 26, 2025 - The `array_shift()` function in PHP removes the first element or value from an array and returns that value.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ php โ€บ how can i get the first element of an array in php?
How can I get the first element of an array in PHP? | Sentry
Here, we remove the last element of our reversed array, which gives us the first element of our original $myArray. The output gives us the value of the first element of our original array and the original array: ... $myArray = [ 'red' => 'apple', 'yellow' => 'banana', 'green' => 'grapes' ]; // get the first element $firstFruit = array_shift($myArray); // output echo $firstFruit .PHP_EOL; print_r($myArray);
๐ŸŒ
Rookienerd
rookienerd.com โ€บ tutorial โ€บ php โ€บ how-to-remove-first-element-from-an-array-in-php
How to remove first element from an array in PHP? - Rookie Nerd
Arraylist: Array ( [0] => Blue [1] => Red [2] => Black [3] => Green [4] => Gray [5] => White ) Removed element from array is: Blue Updated arraylist: Array ( [0] => Red [1] => Black [2] => Green [3] => Gray [4] => White ) ... <?php $game = array(1 => "Carom", 2 => "Chess", 3 => "Ludo"); echo "Removed element: ".array_shift($game).
Top answer
1 of 16
3648

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
]
2 of 16
1429

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)
} */
๐ŸŒ
PHP Tutorial
phptutorial.net โ€บ home โ€บ php tutorial โ€บ php array_shift
PHP array_shift() Function
April 7, 2025 - Summary: in this tutorial, you will learn how to use the PHP array_shift() function to remove an element from the beginning of an array. The array_shift() function removes the first element from an array and returns it.
๐ŸŒ
Studytonight
studytonight.com โ€บ php-howtos โ€บ how-to-remove-the-first-element-from-an-array-in-php
How to remove the first element from an array in PHP? - Studytonight
We can remove the first element of an array using the array_shift() function. This is a predefined PHP function that takes an array as its input and removes the first element of the input array.